public function ParagraphsWidget::elementValidate

File

paragraphs/src/Plugin/Field/FieldWidget/ParagraphsWidget.php, line 2472

Class

ParagraphsWidget
Plugin implementation of the 'entity_reference_revisions paragraphs' widget.

Namespace

Drupal\paragraphs\Plugin\Field\FieldWidget

Code

public function elementValidate($element, FormStateInterface $form_state, $form) {
  $field_name = $this->fieldDefinition
    ->getName();
  $widget_state = static::getWidgetState($element['#field_parents'], $field_name, $form_state);
  $delta = $element['#delta'];
  if (isset($widget_state['paragraphs'][$delta]['entity'])) {

    /** @var \Drupal\paragraphs\ParagraphInterface $paragraphs_entity */
    $entity = $widget_state['paragraphs'][$delta]['entity'];

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
    $display = $widget_state['paragraphs'][$delta]['display'];
    if ($widget_state['paragraphs'][$delta]['mode'] == 'edit') {

      // Extract the form values on submit for getting the current paragraph.
      $display
        ->extractFormValues($entity, $element['subform'], $form_state);

      // Validate all enabled behavior plugins.
      $paragraphs_type = $entity
        ->getParagraphType();
      if (\Drupal::currentUser()
        ->hasPermission('edit behavior plugin settings')) {
        foreach ($paragraphs_type
          ->getEnabledBehaviorPlugins() as $plugin_id => $plugin_values) {
          if (!empty($element['behavior_plugins'][$plugin_id])) {
            $subform_state = SubformState::createForSubform($element['behavior_plugins'][$plugin_id], $form_state
              ->getCompleteForm(), $form_state);
            $plugin_values
              ->validateBehaviorForm($entity, $element['behavior_plugins'][$plugin_id], $subform_state);
          }
        }
      }
    }
  }
  static::setWidgetState($element['#field_parents'], $field_name, $form_state, $widget_state);
}