You are here

public function ParagraphsTableWidget::formMultipleElements in Paragraphs table 8

For multiple elements.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

Overrides ParagraphsWidget::formMultipleElements

File

src/Plugin/Field/FieldWidget/ParagraphsTableWidget.php, line 110

Class

ParagraphsTableWidget
Plugin implementation of the 'paragraphs_table_widget' widget.

Namespace

Drupal\paragraphs_table\Plugin\Field\FieldWidget

Code

public function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
  $elements = parent::formMultipleElements($items, $form, $form_state);
  $target_type = $this
    ->getFieldSetting('target_type');
  $default_type = $this
    ->getDefaultParagraphTypeMachineName();
  $field_name = $this->fieldDefinition
    ->getName();
  $widgetState = static::getWidgetState($this->fieldParents, $field_name, $form_state);
  $widgetState["paragraphs"][0]["mode"] = 'edit';
  $elements["#paragraphsTable"]["#widget_state"] = $widgetState;
  $elements["#paragraphsTable"]["#table_vertical"] = $this
    ->getSetting('vertical');
  $elements["#paragraphsTable"]["#paste_clipboard"] = $this
    ->getSetting('paste_clipboard');
  $elements["#paragraphsTable"]["#show_all"] = $this
    ->getSetting('show_all');
  $elements["#paragraphsTable"]["#feature"] = $this
    ->getSetting('features');
  if (empty($default_type)) {
    $default_type = array_key_first($this
      ->getAllowedTypes());
  }
  if (empty($default_type)) {
    return $elements;
  }
  $formDisplay = \Drupal::service('entity_display.repository')
    ->getFormDisplay($target_type, $default_type);
  $components = $formDisplay
    ->getComponents();
  uasort($components, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');

  /** @var \Drupal\paragraphs\ParagraphInterface $paragraphs_entity */
  $paragraphs_entity = \Drupal::entityTypeManager()
    ->getStorage($target_type)
    ->create([
    'type' => $default_type,
  ]);
  $field_definitions = $paragraphs_entity
    ->getFieldDefinitions();
  foreach ($components as $name => $setting) {
    if (!empty($field_definitions[$name]) && $field_definitions[$name] instanceof FieldConfigInterface) {
      $elements["#paragraphsTable"]['#fields'][$name] = $field_definitions[$name];
    }
  }
  return $elements;
}