Loading comment_limit.module +3 −4 Original line number Diff line number Diff line Loading @@ -81,7 +81,6 @@ function comment_limit_form_comment_form_alter(&$form, FormStateInterface $form_ $field_name ); // Don't validate if user wants to edit his comment. if (!$field_config instanceof FieldConfig) { return; } Loading Loading @@ -127,9 +126,9 @@ function comment_limit_form_comment_form_alter(&$form, FormStateInterface $form_ * * @param string $entity_type * The entity type. * @param FieldConfig $comment * @param \Drupal\field\Entity\FieldConfig $comment * The FieldConfig object of the comment entity. * @param FormStateInterface $form_state * @param \Drupal\Core\Form\FormStateInterface $form_state * The form values from the field config form. */ function comment_limit_form_field_edit_form_add_form_builder($entity_type, FieldConfig $comment, &$form, FormStateInterface $form_state) { Loading Loading @@ -199,7 +198,7 @@ function comment_limit_entity_extra_field_info() { function comment_limit_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { $fields = $entity->getFieldDefinitions(); foreach ($fields as $delta => $field) { foreach ($fields as $field) { if ($field->getType() == 'comment') { $comment_limit = Drupal::service('comment_limit.service'); Loading comment_limit.services.yml +1 −1 Original line number Diff line number Diff line services: comment_limit.service: class: Drupal\comment_limit\CommentLimit arguments: ['@database', '@current_user'] arguments: ['@database', '@current_user', '@entity_type.manager'] src/CommentLimit.php +18 −17 Original line number Diff line number Diff line Loading @@ -4,14 +4,13 @@ namespace Drupal\comment_limit; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\field\Entity\FieldConfig; /** * Class Comment Limit Query. * * @package Drupal\comment_limit */ class CommentLimit { Loading @@ -19,36 +18,38 @@ class CommentLimit { /** * The user object. * * @var AccountProxyInterface $user */ protected $user; protected AccountProxyInterface $user; /** * Database connection. * * @var Connection $database */ protected $database; protected Connection $database; /** * Message to show. * * @var string $message */ protected $message; protected string $message; /** * The entity type manager. */ protected EntityTypeManagerInterface $entityTypeManager; /** * Constructor. * * @param Connection $database * @param \Drupal\Core\Database\Connection $database * The database connection. * @param AccountProxyInterface $user * @param \Drupal\Core\Session\AccountProxyInterface $user * The current user. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. */ public function __construct(Connection $database, AccountProxyInterface $user) { public function __construct(Connection $database, AccountProxyInterface $user, EntityTypeManagerInterface $entity_type_manager) { $this->database = $database; $this->user = $user; $this->entityTypeManager = $entity_type_manager; } /** Loading Loading @@ -130,7 +131,7 @@ class CommentLimit { } /** * Has the user reached his/her comment limit. * Has the user reached comment limit. * * @param int $entity_id * The ID of the current entity. Loading Loading @@ -180,12 +181,12 @@ class CommentLimit { /** * Get all ContentEntityTypes. * * @return array entity types * @return array * Get an array of all ContentEntities. */ public function getAllEntityTypes() { // Get all entities. $entity_types = \Drupal::entityTypeManager()->getDefinitions(); $entity_types = $this->entityTypeManager->getDefinitions(); $content_entity_types = array_filter($entity_types, function ($entity_type) { return $entity_type instanceof ContentEntityTypeInterface; }); Loading src/Plugin/Validation/Constraint/CommentFormConstraint.php +12 −0 Original line number Diff line number Diff line Loading @@ -14,12 +14,24 @@ use Symfony\Component\Validator\Constraint; */ class CommentFormConstraint extends Constraint { /** * Entity id. */ public $entityId; /** * Entity type. */ public $entityType; /** * Field id. */ public $fieldId; /** * Field name. */ public $fieldName; } src/Plugin/Validation/Constraint/CommentFormConstraintValidator.php +4 −7 Original line number Diff line number Diff line Loading @@ -7,13 +7,10 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\ExecutionContextInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Class CommentFormConstraintValidator. * * @package Drupal\comment_limit\Plugin\Validation\Constraint * Comment Form ConstraintValidator. */ class CommentFormConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface { Loading @@ -22,21 +19,21 @@ class CommentFormConstraintValidator extends ConstraintValidator implements Cont /** * Validator 2.5 and upwards compatible execution context. * * @var ExecutionContextInterface * @var \Symfony\Component\Validator\ExecutionContextInterface */ protected $context; /** * Inject comment_limit.service. * * @var CommentLimit $commentLimit * @var \Drupal\comment_limit\CommentLimit */ protected $commentLimit; /** * Constructs a new CommentFormConstraintValidator. * * @param CommentLimit $comment_limit * @param \Drupal\comment_limit\CommentLimit $comment_limit * The comment_limit.service. */ public function __construct(CommentLimit $comment_limit) { Loading Loading
comment_limit.module +3 −4 Original line number Diff line number Diff line Loading @@ -81,7 +81,6 @@ function comment_limit_form_comment_form_alter(&$form, FormStateInterface $form_ $field_name ); // Don't validate if user wants to edit his comment. if (!$field_config instanceof FieldConfig) { return; } Loading Loading @@ -127,9 +126,9 @@ function comment_limit_form_comment_form_alter(&$form, FormStateInterface $form_ * * @param string $entity_type * The entity type. * @param FieldConfig $comment * @param \Drupal\field\Entity\FieldConfig $comment * The FieldConfig object of the comment entity. * @param FormStateInterface $form_state * @param \Drupal\Core\Form\FormStateInterface $form_state * The form values from the field config form. */ function comment_limit_form_field_edit_form_add_form_builder($entity_type, FieldConfig $comment, &$form, FormStateInterface $form_state) { Loading Loading @@ -199,7 +198,7 @@ function comment_limit_entity_extra_field_info() { function comment_limit_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { $fields = $entity->getFieldDefinitions(); foreach ($fields as $delta => $field) { foreach ($fields as $field) { if ($field->getType() == 'comment') { $comment_limit = Drupal::service('comment_limit.service'); Loading
comment_limit.services.yml +1 −1 Original line number Diff line number Diff line services: comment_limit.service: class: Drupal\comment_limit\CommentLimit arguments: ['@database', '@current_user'] arguments: ['@database', '@current_user', '@entity_type.manager']
src/CommentLimit.php +18 −17 Original line number Diff line number Diff line Loading @@ -4,14 +4,13 @@ namespace Drupal\comment_limit; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\field\Entity\FieldConfig; /** * Class Comment Limit Query. * * @package Drupal\comment_limit */ class CommentLimit { Loading @@ -19,36 +18,38 @@ class CommentLimit { /** * The user object. * * @var AccountProxyInterface $user */ protected $user; protected AccountProxyInterface $user; /** * Database connection. * * @var Connection $database */ protected $database; protected Connection $database; /** * Message to show. * * @var string $message */ protected $message; protected string $message; /** * The entity type manager. */ protected EntityTypeManagerInterface $entityTypeManager; /** * Constructor. * * @param Connection $database * @param \Drupal\Core\Database\Connection $database * The database connection. * @param AccountProxyInterface $user * @param \Drupal\Core\Session\AccountProxyInterface $user * The current user. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. */ public function __construct(Connection $database, AccountProxyInterface $user) { public function __construct(Connection $database, AccountProxyInterface $user, EntityTypeManagerInterface $entity_type_manager) { $this->database = $database; $this->user = $user; $this->entityTypeManager = $entity_type_manager; } /** Loading Loading @@ -130,7 +131,7 @@ class CommentLimit { } /** * Has the user reached his/her comment limit. * Has the user reached comment limit. * * @param int $entity_id * The ID of the current entity. Loading Loading @@ -180,12 +181,12 @@ class CommentLimit { /** * Get all ContentEntityTypes. * * @return array entity types * @return array * Get an array of all ContentEntities. */ public function getAllEntityTypes() { // Get all entities. $entity_types = \Drupal::entityTypeManager()->getDefinitions(); $entity_types = $this->entityTypeManager->getDefinitions(); $content_entity_types = array_filter($entity_types, function ($entity_type) { return $entity_type instanceof ContentEntityTypeInterface; }); Loading
src/Plugin/Validation/Constraint/CommentFormConstraint.php +12 −0 Original line number Diff line number Diff line Loading @@ -14,12 +14,24 @@ use Symfony\Component\Validator\Constraint; */ class CommentFormConstraint extends Constraint { /** * Entity id. */ public $entityId; /** * Entity type. */ public $entityType; /** * Field id. */ public $fieldId; /** * Field name. */ public $fieldName; }
src/Plugin/Validation/Constraint/CommentFormConstraintValidator.php +4 −7 Original line number Diff line number Diff line Loading @@ -7,13 +7,10 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\ExecutionContextInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Class CommentFormConstraintValidator. * * @package Drupal\comment_limit\Plugin\Validation\Constraint * Comment Form ConstraintValidator. */ class CommentFormConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface { Loading @@ -22,21 +19,21 @@ class CommentFormConstraintValidator extends ConstraintValidator implements Cont /** * Validator 2.5 and upwards compatible execution context. * * @var ExecutionContextInterface * @var \Symfony\Component\Validator\ExecutionContextInterface */ protected $context; /** * Inject comment_limit.service. * * @var CommentLimit $commentLimit * @var \Drupal\comment_limit\CommentLimit */ protected $commentLimit; /** * Constructs a new CommentFormConstraintValidator. * * @param CommentLimit $comment_limit * @param \Drupal\comment_limit\CommentLimit $comment_limit * The comment_limit.service. */ public function __construct(CommentLimit $comment_limit) { Loading