class CommentManager implements CommentManagerInterface (View source)

Comment manager contains common functions to manage comment fields.

Traits

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

Properties

protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected EntityFieldManagerInterface $entityFieldManager

The entity field manager.

protected EntityDisplayRepositoryInterface $entityDisplayRepository

The entity display repository.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

protected bool $authenticatedCanPostComments

Whether the \Drupal\user\RoleInterface::AUTHENTICATED_ID can post comments.

protected Config $userConfig

The user settings config object.

protected ModuleHandlerInterface $moduleHandler

The module handler service.

protected AccountInterface $currentUser

The current user.

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.

__construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager, EntityDisplayRepositoryInterface $entity_display_repository)

Construct the CommentManager object.

array
getFields(string $entity_type_id)

Utility function to return an array of comment fields.

addBodyField($comment_type_id)

Creates a comment_body field.

string
forbiddenMessage(EntityInterface $entity, string $field_name)

Provides a message if posting comments is forbidden.

int|false
getCountNewComments(EntityInterface $entity, string $field_name = NULL, int $timestamp = 0)

Returns the number of new comments available on a given entity for a user.

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

__construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager, EntityDisplayRepositoryInterface $entity_display_repository)

Construct the CommentManager object.

Parameters

EntityTypeManagerInterface $entity_type_manager

The entity type manager service.

ConfigFactoryInterface $config_factory

The config factory.

TranslationInterface $string_translation

The string translation service.

ModuleHandlerInterface $module_handler

The module handler service.

AccountInterface $current_user

The current user.

EntityFieldManagerInterface $entity_field_manager

The entity field manager service.

EntityDisplayRepositoryInterface $entity_display_repository

The entity display repository service.

array getFields(string $entity_type_id)

Utility function to return an array of comment fields.

Parameters

string $entity_type_id

The content entity type to which the comment fields are attached.

Return Value

array

An array of comment field map definitions, keyed by field name. Each value is an array with two entries:

  • type: The field type.
  • bundles: The bundles in which the field appears, as an array with entity types as keys and the array of bundle names as values.

addBodyField($comment_type_id)

Creates a comment_body field.

Parameters

$comment_type_id

string forbiddenMessage(EntityInterface $entity, string $field_name)

Provides a message if posting comments is forbidden.

If authenticated users can post comments, a message is returned that prompts the anonymous user to log in (or register, if applicable) that redirects to entity comment form. Otherwise, no message is returned.

Parameters

EntityInterface $entity

The entity to which comments are attached to.

string $field_name

The field name on the entity to which comments are attached to.

Return Value

string

HTML for a "you can't post comments" notice.

int|false getCountNewComments(EntityInterface $entity, string $field_name = NULL, int $timestamp = 0)

Returns the number of new comments available on a given entity for a user.

Parameters

EntityInterface $entity

The entity to which the comments are attached to.

string $field_name

(optional) The field_name to count comments for. Defaults to any field.

int $timestamp

(optional) Time to count from. Defaults to time of last user access the entity.

Return Value

int|false

The number of new comments or FALSE if the user is not authenticated.