You are here

public function ParagraphCloneForm::validateForm in Paragraphs Edit 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/ParagraphCloneForm.php, line 142

Class

ParagraphCloneForm

Namespace

Drupal\paragraphs_edit

Code

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

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