class CommentViewBuilder extends EntityViewBuilder (View source)

View builder handler for comments.

Traits

Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.

Provides dependency injection friendly methods for serialization.

Properties

protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected ModuleHandlerInterface $moduleHandler

The module handler to invoke hooks on.

from  EntityHandlerBase
protected string $entityTypeId

The type of entities for which this view builder is instantiated.

from  EntityViewBuilder
protected EntityTypeInterface $entityType

Information about the entity type.

from  EntityViewBuilder
protected EntityRepositoryInterface $entityRepository

The entity repository service.

from  EntityViewBuilder
protected EntityDisplayRepositoryInterface $entityDisplayRepository

The entity display repository.

from  EntityViewBuilder
protected string $cacheBin

The cache bin used to store the render cache.

from  EntityViewBuilder
protected LanguageManagerInterface $languageManager

The language manager.

from  EntityViewBuilder
protected Registry $themeRegistry

The theme registry.

from  EntityViewBuilder
protected EntityViewDisplayInterface[] $singleFieldDisplays

The EntityViewDisplay objects created for individual field rendering.

from  EntityViewBuilder
protected AccountInterface $currentUser

The current user.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

Methods

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

getStringTranslation()

Gets the string translation service.

$this
setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

moduleHandler()

Gets the module handler.

$this
setModuleHandler(ModuleHandlerInterface $module_handler)

Sets the module handler for this handler.

__construct(EntityTypeInterface $entity_type, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, AccountInterface $current_user, Registry $theme_registry, EntityDisplayRepositoryInterface $entity_display_repository, EntityTypeManagerInterface $entity_type_manager)

Constructs a new CommentViewBuilder.

createInstance(ContainerInterface $container, EntityTypeInterface $entity_type)

Instantiates a new instance of this entity handler.

array
view(EntityInterface $entity, string $view_mode = 'full', string $langcode = NULL)

Builds the render array for the provided entity.

static string[]
trustedCallbacks()

Lists the trusted callbacks provided by the implementing class.

A
viewMultiple(array $entities = [], string $view_mode = 'full', string $langcode = NULL)

Builds the render array for the provided entities.

array
getBuildDefaults(EntityInterface $entity, string $view_mode)

Provides entity-specific defaults to the build process.

array
build(array $build)

Builds an entity's view; augments entity defaults.

array
buildMultiple(array $build_list)

Builds multiple entities' views; augments entity defaults.

buildComponents(array $build, array $entities, array $displays, string $view_mode)

Builds the component fields and properties of a set of entities.

addContextualLinks(array $build, EntityInterface $entity)

Add contextual links.

alterBuild(array $build, EntityInterface $comment, EntityViewDisplayInterface $display, string $view_mode)

Specific per-entity building.

array
getCacheTags()

The cache tag associated with this entity view builder.

resetCache(array $entities = NULL)

Resets the entity render cache.

bool
isViewModeCacheable(string $view_mode)

Determines whether the view mode is cacheable.

array
viewField(FieldItemListInterface $items, string|array $display_options = [])

Builds a renderable array for the value of a single field in an entity.

array
viewFieldItem(FieldItemInterface $item, $display = [])

Builds a renderable array for a single field item.

getSingleFieldDisplay(EntityInterface $entity, string $field_name, string|array $display_options)

Gets an EntityViewDisplay for rendering an individual field.

Details

protected TranslatableMarkup t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

protected ModuleHandlerInterface moduleHandler()

Gets the module handler.

Return Value

ModuleHandlerInterface

The module handler.

$this setModuleHandler(ModuleHandlerInterface $module_handler)

Sets the module handler for this handler.

Parameters

ModuleHandlerInterface $module_handler

The module handler.

Return Value

$this

__construct(EntityTypeInterface $entity_type, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, AccountInterface $current_user, Registry $theme_registry, EntityDisplayRepositoryInterface $entity_display_repository, EntityTypeManagerInterface $entity_type_manager)

Constructs a new CommentViewBuilder.

Parameters

EntityTypeInterface $entity_type

The entity type definition.

EntityRepositoryInterface $entity_repository

The entity repository service.

LanguageManagerInterface $language_manager

The language manager.

AccountInterface $current_user

The current user.

Registry $theme_registry

The theme registry.

EntityDisplayRepositoryInterface $entity_display_repository

The entity display repository.

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

static EntityHandlerInterface createInstance(ContainerInterface $container, EntityTypeInterface $entity_type)

Instantiates a new instance of this entity handler.

This is a factory method that returns a new instance of this object. The factory should pass any needed dependencies into the constructor of this object, but not the container itself. Every call to this method must return a new instance of this object; that is, it may not implement a singleton.

Parameters

ContainerInterface $container

The service container this object should use.

EntityTypeInterface $entity_type

The entity type definition.

Return Value

EntityHandlerInterface

A new instance of the entity handler.

array view(EntityInterface $entity, string $view_mode = 'full', string $langcode = NULL)

Builds the render array for the provided entity.

Parameters

EntityInterface $entity

The entity to render.

string $view_mode

(optional) The view mode that should be used to render the entity.

string $langcode

(optional) For which language the entity should be rendered, defaults to the current content language.

Return Value

array

A render array for the entity.

Exceptions

InvalidArgumentException

static string[] trustedCallbacks()

Lists the trusted callbacks provided by the implementing class.

Trusted callbacks are public methods on the implementing class and can be invoked via \Drupal\Core\Security\DoTrustedCallbackTrait::doTrustedCallback().

Return Value

string[]

List of method names implemented by the class that can be used as trusted callbacks.

A viewMultiple(array $entities = [], string $view_mode = 'full', string $langcode = NULL)

Builds the render array for the provided entities.

Parameters

array $entities

An array of entities implementing EntityInterface to view.

string $view_mode

(optional) The view mode that should be used to render the entity.

string $langcode

(optional) For which language the entity should be rendered, defaults to the current content language.

Return Value

A

render array for the entities, indexed by the same keys as the entities array passed in $entities.

Exceptions

InvalidArgumentException

protected array getBuildDefaults(EntityInterface $entity, string $view_mode)

Provides entity-specific defaults to the build process.

Parameters

EntityInterface $entity

The entity for which the defaults should be provided.

string $view_mode

The view mode that should be used.

Return Value

array

array build(array $build)

Builds an entity's view; augments entity defaults.

This function is assigned as a #pre_render callback in ::view().

It transforms the renderable array for a single entity to the same structure as if we were rendering multiple entities, and then calls the default ::buildMultiple() #pre_render callback.

Parameters

array $build

A renderable array containing build information and context for an entity view.

Return Value

array

The updated renderable array.

See also

RendererInterface::render

array buildMultiple(array $build_list)

Builds multiple entities' views; augments entity defaults.

This function is assigned as a #pre_render callback in ::viewMultiple().

By delaying the building of an entity until the #pre_render processing in \Drupal::service('renderer')->render(), the processing cost of assembling an entity's renderable array is saved on cache-hit requests.

Parameters

array $build_list

A renderable array containing build information and context for an entity view.

Return Value

array

The updated renderable array.

See also

RendererInterface::render

buildComponents(array $build, array $entities, array $displays, string $view_mode)

Builds the component fields and properties of a set of entities.

Parameters

array $build

The renderable array representing the entity content.

array $entities

The entities whose content is being built.

array $displays

The array of entity view displays holding the display options configured for the entity components, keyed by bundle name.

string $view_mode

The view mode in which the entity is being viewed.

Add contextual links.

Parameters

array $build

The render array that is being created.

EntityInterface $entity

The entity to be prepared.

protected alterBuild(array $build, EntityInterface $comment, EntityViewDisplayInterface $display, string $view_mode)

Specific per-entity building.

Parameters

array $build

The render array that is being created.

EntityInterface $comment
EntityViewDisplayInterface $display

The entity view display holding the display options configured for the entity components.

string $view_mode

The view mode that should be used to prepare the entity.

array getCacheTags()

The cache tag associated with this entity view builder.

An entity view builder is instantiated on a per-entity type basis, so the cache tags are also per-entity type.

Return Value

array

An array of cache tags.

resetCache(array $entities = NULL)

Resets the entity render cache.

Parameters

array $entities

(optional) If specified, the cache is reset for the given entities only.

protected bool isViewModeCacheable(string $view_mode)

Determines whether the view mode is cacheable.

Parameters

string $view_mode

Name of the view mode that should be rendered.

Return Value

bool

TRUE if the view mode can be cached, FALSE otherwise.

array viewField(FieldItemListInterface $items, string|array $display_options = [])

Builds a renderable array for the value of a single field in an entity.

The resulting output is a fully themed field with label and multiple values.

This function can be used by third-party modules that need to output an isolated field.

  • Do not use inside node (or any other entity) templates; use render($content[FIELD_NAME]) instead.
  • The FieldItemInterface::view() method can be used to output a single formatted field value, without label or wrapping field markup.

The function takes care of invoking the prepare_view steps. It also respects field access permissions.

Parameters

FieldItemListInterface $items

FieldItemList containing the values to be displayed.

string|array $display_options

Can be either:

  • The name of a view mode. The field will be displayed according to the display settings specified for this view mode in the $field definition for the field in the entity's bundle. If no display settings are found for the view mode, the settings for the 'default' view mode will be used.
  • An array of display options. The following key/value pairs are allowed:
    • label: (string) Position of the label. The default 'field' theme implementation supports the values 'inline', 'above' and 'hidden'. Defaults to 'above'.
    • type: (string) The formatter to use. Defaults to the 'default_formatter' for the field type. The default formatter will also be used if the requested formatter is not available.
    • settings: (array) Settings specific to the formatter. Defaults to the formatter's default settings.
    • weight: (float) The weight to assign to the renderable element. Defaults to 0.

Return Value

array

A renderable array for the field values.

array viewFieldItem(FieldItemInterface $item, $display = [])

Builds a renderable array for a single field item.

Parameters

FieldItemInterface $item

FieldItem to be displayed.

$display

Return Value

array

A renderable array for the field item.

protected EntityViewDisplayInterface getSingleFieldDisplay(EntityInterface $entity, string $field_name, string|array $display_options)

Gets an EntityViewDisplay for rendering an individual field.

Parameters

EntityInterface $entity

The entity.

string $field_name

The field name.

string|array $display_options

The display options passed to the viewField() method.

Return Value

EntityViewDisplayInterface