You are here

public function ParagraphCloneForm::validateForm in Paragraphs table 8

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides ContentEntityForm::validateForm

File

src/Form/ParagraphCloneForm.php, line 83

Class

ParagraphCloneForm
Paragraph Clone Form class.

Namespace

Drupal\paragraphs_table\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $destination_entity_type = $form_state
    ->getValue([
    'entity_type',
  ]);
  $destination_entity_id = $form_state
    ->getValue([
    'parent',
  ]);
  $destination_field = $form_state
    ->getValue([
    'field',
  ]);
  if ($destination_entity_id && $destination_field) {

    /** @var \Drupal\Core\Entity\FieldableEntityInterface $destination_entity */
    $destination_entity = $this->entityTypeManager
      ->getStorage($destination_entity_type)
      ->load($destination_entity_id);
    if ($destination_entity) {
      if (!$destination_entity
        ->access('update')) {
        $form_state
          ->setError($form['parent'], 'You are not allowed to update this content.');
      }
      if (!$destination_entity
        ->get($destination_field)
        ->access('edit')) {
        $form_state
          ->setError($form['field'], 'You are not allowed to edit this field.');
      }
    }
  }
  return parent::validateForm($form, $form_state);
}