You are here

public function ParagraphsBrowserWidgetTrait::formMultipleElements in Paragraphs Browser 8

File

src/Plugin/Field/FieldWidget/ParagraphsBrowserWidgetTrait.php, line 75
Paragraphs Previewer widget trait implementation for paragraphs.

Class

ParagraphsBrowserWidgetTrait
Trait of Plugin implementation of the 'entity_reference paragraphs' widget.

Namespace

Drupal\paragraphs_browser\Plugin\Field\FieldWidget

Code

public function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
  $elements = parent::formMultipleElements($items, $form, $form_state);

  // If the Paragraph browser hasn't been set, return un-modified form.
  if ($this
    ->getSetting('paragraphs_browser') == '_na') {
    return $elements;
  }

  //    $elements['add_more'] = array(
  //      '#type' => 'container',
  //      '#theme_wrappers' => array('paragraphs_dropbutton_wrapper'),
  //    );
  if (empty($this->uuid)) {
    $this->uuid = \Drupal::service('uuid')
      ->generate();
  }
  $elements['add_more']['add_more_select']['#attributes']['data-uuid'] = $this->uuid;
  $elements['add_more']['add_more_select']['#attributes']['class'][] = 'js-hide';
  $elements['add_more']['add_more_select']['#title_display'] = 'hidden';
  $elements['add_more']['add_more_button']['#attributes']['data-uuid'] = $this->uuid;
  $elements['add_more']['add_more_button']['#attributes']['class'][] = 'js-hide';
  unset($elements['add_more']['add_more_button']['#suffix']);
  unset($elements['add_more']['add_more_button']['#prefix']);
  $elements['#attached']['library'][] = 'paragraphs_browser/modal';
  $storage = $form_state
    ->getStorage();
  if (!$this->fieldDefinition
    ->isTranslatable() && $storage['langcode'] !== $storage['entity_default_langcode']) {
    unset($elements['add_more']);
  }
  else {
    $elements['add_more']['browse'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Add :title', [
        ':title' => $this
          ->getSetting('title'),
      ]),
      '#attributes' => [
        'class' => [
          'js-show',
        ],
      ],
      '#ajax' => [
        'url' => Url::fromRoute('paragraphs_browser.paragraphs_browser_controller', [
          'field_config' => implode('.', array(
            $items
              ->getEntity()
              ->getEntityTypeId(),
            $items
              ->getEntity()
              ->bundle(),
            $this->fieldDefinition
              ->getName(),
          )),
          'paragraphs_browser_type' => $this
            ->getSetting('paragraphs_browser'),
          'uuid' => $this->uuid,
        ]),
      ],
    );
  }
  if (isset($elements['#cardinality']) && $elements['#cardinality'] != -1) {
    $keyCount = count(array_filter(array_keys($elements), 'is_numeric'));
    if ($elements['#cardinality'] <= $keyCount) {
      unset($elements['add_more']);
    }
  }
  return $elements;
}