You are here

public function SynonymForm::form in Synonyms 8

Same name and namespace in other branches
  1. 2.0.x src/Form/SynonymForm.php \Drupal\synonyms\Form\SynonymForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/SynonymForm.php, line 142

Class

SynonymForm
Entity form for 'synonym' config entity type.

Namespace

Drupal\synonyms\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $class = get_class($this);
  $provider_plugin = $this->entity
    ->getProviderPlugin();
  if ($form_state
    ->getValue('provider_plugin')) {
    $provider_plugin = $form_state
      ->getValue('provider_plugin');
  }
  $form['id'] = [
    '#type' => 'value',
    '#value' => str_replace(':', '.', $provider_plugin),
  ];
  $options = [];
  foreach ($this->synonymsProviderPluginManager
    ->getDefinitions() as $plugin_id => $plugin) {
    if ($plugin['controlled_entity_type'] == $this->controlledEntityType && $plugin['controlled_bundle'] == $this->controlledBundle && $plugin['synonyms_behavior_service'] == $this->behaviorServiceId) {
      $options[$plugin_id] = $plugin['label'];
    }
  }
  $form['provider_plugin'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Synonyms provider'),
    '#description' => $this
      ->t('Select what synonyms provider it should represent.'),
    '#required' => TRUE,
    '#options' => $options,
    '#default_value' => $this->entity
      ->getProviderPlugin(),
    '#ajax' => [
      'wrapper' => 'synonyms-entity-configuration-ajax-wrapper',
      'event' => 'change',
      'callback' => [
        $class,
        'ajaxForm',
      ],
    ],
  ];
  $form['ajax_wrapper'] = [
    '#prefix' => '<div id="synonyms-entity-configuration-ajax-wrapper">',
    '#suffix' => '</div>',
  ];
  $form['ajax_wrapper']['provider_configuration'] = [
    '#tree' => TRUE,
    '#title' => $this
      ->t('Provider settings'),
    '#open' => TRUE,
  ];
  $form['ajax_wrapper']['behavior_configuration'] = [
    '#tree' => TRUE,
    '#title' => $this
      ->t('Behavior settings'),
    '#open' => TRUE,
  ];
  if ($provider_plugin) {
    $provider_plugin_instance = $this->entity
      ->getProviderPluginInstance();
    if ($provider_plugin_instance instanceof PluginFormInterface) {
      $form['ajax_wrapper']['provider_configuration']['#type'] = 'details';
      $form['ajax_wrapper']['provider_configuration'] += $provider_plugin_instance
        ->buildConfigurationForm($form['ajax_wrapper']['provider_configuration'], $form_state);
    }
    $behavior_service_instance = $provider_plugin_instance
      ->getBehaviorServiceInstance();
    if ($behavior_service_instance instanceof SynonymsBehaviorConfigurableInterface) {
      $form['ajax_wrapper']['behavior_configuration']['#type'] = 'details';
      $form['ajax_wrapper']['behavior_configuration'] += $behavior_service_instance
        ->buildConfigurationForm($form['ajax_wrapper']['behavior_configuration'], $form_state, $this->entity
        ->getBehaviorConfiguration(), $this->entity);
    }
  }
  return $form;
}