Repository: wagtail/wagtail
File: wagtail/blocks/base.py (Python)

Code before:
        # will be used for a complete view, not just the block. Instead, the
        # default preview context uses `{% include_block %}`, which will use
        # `get_context`.
        return parent_context or {}

    def get_preview_template(self, value, context=None):
        # We do not fall back to `get_template` here, because the template will
        # be used for a complete view, not just the block. In most cases, the
        # block's template cannot stand alone for the preview, as it would be
        # missing the necessary static assets.
        #
        # Instead, the default preview template uses `{% include_block %}`,
        # which will use `get_template` if a template is defined.
        return (
            getattr(self.meta, "preview_template", None)
            or self.DEFAULT_PREVIEW_TEMPLATE
        )

    def get_preview_value(self):
        if hasattr(self.meta, "preview_value"):
            return self.normalize(self.meta.preview_value)
        return self.get_default()

    @cached_property
    def _has_default(self):
        return getattr(self.meta, "default", None) is not None

    @cached_property
    def is_previewable(self):
        # To prevent showing a broken preview if the block preview has not been
        # configured, consider the block to be previewable if either:
        # - a specific preview template is configured for the block
        # - a preview value is provided and the global template has been overridden
        # which are the intended ways to configure block previews.
        #
        # If a block is made previewable by other means, the `is_previewable`
        # property should be overridden to return `True`.
        has_specific_template = (
            hasattr(self.meta, "preview_template")
            or self.__class__.get_preview_template is not Block.get_preview_template
        )
        has_preview_value = (
            hasattr(self.meta, "preview_value")
            or self._has_default
            or self.__class__.get_preview_context is not Block.get_preview_context
            or self.__class__.get_preview_value is not Block.get_preview_value
        )
        has_global_template = template_is_overridden(
            self.DEFAULT_PREVIEW_TEMPLATE,
            "templates",
        )

Review comment (suggestion):
Probably makes sense to name the argument `parent_context` for this reason, but I named it `context` to match the signature of `get_template()`...

(The reason why this is the case, if you're curious, is because the template is used by the preview view. In a typical Django view, the template is resolved before `get_context_data()` is called, so we can't get the final context here.)

Code after:
        when rendering the block's preview. The ``value`` argument is the value
        returned by :meth:`get_preview_value`. The ``parent_context`` argument
        contains the following variables:

        - ``request``: The current request object.
        - ``block_def``: The block instance.
        - ``block_class``: The block class.
        - ``bound_block``: A ``BoundBlock`` instance representing the block and its value.

        If :ref:`the global preview template <streamfield_global_preview_template>`
        is used, the block will be rendered as the main content using
        ``{% include_block %}``, which in turn uses :meth:`get_context`. As a
        result, the context returned by this method will be available as the
        ``parent_context`` for ``get_context()`` when the preview is rendered.
        """
        # NOTE: see StreamFieldBlockPreview.base_context for the context variables
        # that can be documented.
        return parent_context or {}

    def get_preview_template(self, value, context=None):
        """
        Return the template to use for rendering the block's preview. The ``value``
        argument is the value returned by :meth:`get_preview_value`. The ``context``
        argument contains the variables listed for the ``parent_context`` argument
        of :meth:`get_preview_context` above (and not the context returned by that
        method itself).

        Note that the preview template is used to render a complete HTML page of
        the preview, not just an HTML fragment for the block. The method returns
        the ``preview_template`` attribute from the block's options if provided,
        and falls back to
        :ref:`the global preview template <streamfield_global_preview_template>`
        otherwise.

        If the global preview template is used, the block will be rendered as the
        main content using ``{% include_block %}``, which in turn uses
        :meth:`get_template`.
        """
        return (
            getattr(self.meta, "preview_template", None)
            or self.DEFAULT_PREVIEW_TEMPLATE
        )

    def get_preview_value(self):
        """
        Return the placeholder value that will be used for rendering the block's
        preview. By default, the value is the ``preview_value`` from the block's
        options if provided, otherwise the ``default`` is used as fallback. This
        method can be overridden to provide a dynamic preview value, such as
        from the database.
        """