You are here

class WorkflowFieldConstraintValidator in Workflow 8

Validates the CommentName constraint.

Hierarchy

Expanded class hierarchy of WorkflowFieldConstraintValidator

See also

https://drupalwatchdog.com/volume-5/issue-2/introducing-drupal-8s-entity...

File

src/Plugin/Validation/Constraint/WorkflowFieldConstraintValidator.php, line 17

Namespace

Drupal\workflow\Plugin\Validation\Constraint
View source
class WorkflowFieldConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

  /**
   * Validator 2.5 and upwards compatible execution context.
   *
   * @var \Symfony\Component\Validator\Context\ExecutionContextInterface
   */
  protected $context;

  /**
   * User storage handler.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * Constructs a new Validator.
   *
   * @param \Drupal\user\UserStorageInterface $user_storage
   *   The user storage handler.
   */
  public function __construct(UserStorageInterface $user_storage) {
    $this->userStorage = $user_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager')
      ->getStorage('user'));
  }

  /**
   * {@inheritdoc}
   */
  public function validate($entity, Constraint $constraint) {

    // Workflow field name on CommentForm has special requirements.

    /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
    $field_storage = $entity
      ->getFieldDefinition()
      ->getFieldStorageDefinition();
    if (!$this
      ->isValidFieldname($field_storage)) {
      $this->context
        ->buildViolation($constraint->messageFieldname)
        ->atPath('fieldnameOnComment')
        ->addViolation();
    }
  }

  /**
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage
   *
   * @return bool
   */
  protected function isValidFieldname(FieldStorageDefinitionInterface $field_storage) {
    if ($field_storage
      ->getTargetEntityTypeId() !== 'comment') {
      return TRUE;
    }
    $field_name = $field_storage
      ->getName();

    // Check if the 'comment' field name exists on the 'commented' entity type.
    // @todo Fix field on a non-relevant entity_type.
    $comment_field_name_ok = FALSE;
    foreach (_workflow_info_fields() as $info) {
      if ($info
        ->getName() == $field_name && $info
        ->getTargetEntityTypeId() !== 'comment') {
        $comment_field_name_ok = TRUE;
      }
    }
    return $comment_field_name_ok;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WorkflowFieldConstraintValidator::$context protected property Validator 2.5 and upwards compatible execution context.
WorkflowFieldConstraintValidator::$userStorage protected property User storage handler.
WorkflowFieldConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
WorkflowFieldConstraintValidator::isValidFieldname protected function
WorkflowFieldConstraintValidator::validate public function Checks if the passed value is valid.
WorkflowFieldConstraintValidator::__construct public function Constructs a new Validator.