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

Code before:
    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",
        )
        return has_specific_template or (has_preview_value and has_global_template)

    def get_description(self):
        return getattr(self.meta, "description", "")

    def get_api_representation(self, value, context=None):
        """
        Can be used to customise the API response and defaults to the value returned by get_prep_value.
        """
        return self.get_prep_value(value)

    def render_basic(self, value, context=None):
        """
        Return a text rendering of 'value', suitable for display on templates. render() will fall back on
        this if the block does not define a 'template' property.
        """
        return force_str(value)

    def get_searchable_content(self, value):
        """
        Returns a list of strings containing text content within this block to be used in a search engine.
        """
        return []


Review comment (suggestion):
Now I wonder whether we should've allowed the `preview_value` to be a callable as well, so people can just pass a function instead of having to make a subclass and override the method, especially for blocks like `ImageBlock`.

Code after:
        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.
        """
        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")