You are here

public function FormElementComponentType::addComponentForm in Flexiform 8

Get a form for adding a component of this type.

Parameters

array $form: The part of the form array that should contain the component plugin settings.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array A form array for setting the options of a component. Normally broken into settings and third_party_settings.

Overrides FormComponentTypeCreateableBase::addComponentForm

File

src/Plugin/FormComponentType/FormElementComponentType.php, line 82

Class

FormElementComponentType
Plugin for field widget component types.

Namespace

Drupal\flexiform\Plugin\FormComponentType

Code

public function addComponentForm(array $form, FormStateInterface $form_state) {

  // Build the parents for the form element selector.
  $parents = $form['#parents'];
  $parents[] = 'form_element';
  $available_plugins = $this->pluginManager
    ->getDefinitionsForContexts($this
    ->getFormEntityManager($form, $form_state)
    ->getContexts());
  $form['#prefix'] = '<div id="flexiform-form-element-add-wrapper">';
  $form['#suffix'] = '</div>';
  $plugin_options = [];
  foreach ($available_plugins as $plugin_id => $plugin_definition) {
    if (empty($plugin_definition['no_ui'])) {
      $plugin_options[$plugin_id] = $plugin_definition['label'];
    }
  }
  $form['form_element'] = [
    '#type' => 'select',
    '#required' => TRUE,
    '#options' => $plugin_options,
    '#title' => $this
      ->t('Form Element'),
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxFormElementSelect',
      ],
      'wrapper' => 'flexiform-form-element-add-wrapper',
    ],
  ];
  if ($plugin_id = NestedArray::getValue($form_state
    ->getUserInput(), $parents)) {
    $plugin = $this->pluginManager
      ->createInstance($plugin_id);
    if ($plugin instanceof ContextAwarePluginInterface) {
      $contexts = $this
        ->getFormEntityManager()
        ->getContexts();
      $form['context_mapping'] = [
        '#parents' => [
          'options',
          'settings',
          'context_mapping',
        ],
      ] + $this
        ->addContextAssignmentElement($plugin, $contexts);
      foreach (Element::children($form['context_mapping']) as $mapping_key) {
        $form['context_mapping'][$mapping_key]['#empty_option'] = $this
          ->t('- Select -');
      }
    }
    $form += $plugin
      ->settingsForm($form, $form_state);
  }
  return $form;
}