You are here

public function BricksTreeDynamicInlineWidget::formElement in Bricks​ 2.x

Same name and namespace in other branches
  1. 8 modules/bricks_dynamic/src/Plugin/Field/FieldWidget/BricksTreeDynamicInlineWidget.php \Drupal\bricks_dynamic\Plugin\Field\FieldWidget\BricksTreeDynamicInlineWidget::formElement()

File

modules/bricks_dynamic/src/Plugin/Field/FieldWidget/BricksTreeDynamicInlineWidget.php, line 168

Class

BricksTreeDynamicInlineWidget
Plugin annotation @FieldWidget( id = "bricks_tree_dynamic_inline", label = @Translation("Bricks tree Dynamic (Inline entity form)"), description = @Translation("A tree of inline entity forms."), field_types = { "bricks_dynamic" }, …

Namespace

Drupal\bricks_dynamic\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  $element = parent::formElement($items, $delta, $element, $form, $form_state);
  $element['entities']['#widget'] = 'bricks_tree_inline';
  $allowed_entity_types = DynamicEntityReferenceItem::getTargetTypes($this
    ->getFieldSettings());
  $entity_type_definitions = \Drupal::entityTypeManager()
    ->getDefinitions();
  $entity_type_options = [];
  foreach ($allowed_entity_types as $allowed_entity_type) {
    if (isset($entity_type_definitions[$allowed_entity_type])) {
      $entity_type_options[$allowed_entity_type] = $entity_type_definitions[$allowed_entity_type]
        ->getLabel();
    }
  }
  $element['actions']['entity_type'] = [
    '#type' => 'select',
    '#options' => $entity_type_options,
    '#default_value' => $this
      ->getEntityType(),
    '#weight' => -9,
    '#ajax' => [
      'callback' => [
        $this,
        'rebuild',
      ],
      'wrapper' => 'inline-entity-form-' . $element['#ief_id'],
    ],
  ];
  $element['actions']['bundle']['#element_validate'] = [
    [
      $this,
      'correctBundle',
    ],
  ];
  $entities = $form_state
    ->get([
    'inline_entity_form',
    $this
      ->getIefId(),
    'entities',
  ]);
  foreach ($entities as $delta => $value) {
    _bricks_form_element_alter($element['entities'][$delta], $items[$delta], $value['entity']);
  }
  return $element;
}