You are here

protected function KeywordSettingsForm::buildAddNew in Alinks 8

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

1 call to KeywordSettingsForm::buildAddNew()
KeywordSettingsForm::buildForm in src/Form/KeywordSettingsForm.php
Form constructor.

File

src/Form/KeywordSettingsForm.php, line 156

Class

KeywordSettingsForm
Class KeywordSettingsForm.

Namespace

Drupal\alinks\Form

Code

protected function buildAddNew(FormStateInterface $form_state) {
  $form = [];
  $entityTypes = [];
  foreach ($this->entityTypeManager
    ->getDefinitions() as $definition) {
    if ($definition instanceof ContentEntityTypeInterface) {
      $entityTypes[$definition
        ->id()] = $definition
        ->getLabel();
    }
  }
  asort($entityTypes);
  $form['entityType'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity type'),
    '#options' => $entityTypes,
    '#empty_value' => '',
    '#empty_option' => $this
      ->t('-- None --'),
    '#ajax' => [
      'callback' => array(
        $this,
        'populateEntitySettings',
      ),
      'wrapper' => 'entity-wrapper',
    ],
    '#group' => 'display',
  ];
  $form['entity'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="entity-wrapper">',
    '#suffix' => '</div>',
    '#group' => 'display',
  );
  $form['entity']['entityBundle'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Entity bundle'),
    '#empty_value' => '',
    '#empty_option' => $this
      ->t('-- None --'),
    '#options' => [],
  );
  $form['entity']['entityDisplay'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Entity display'),
    '#empty_value' => '',
    '#empty_option' => $this
      ->t('-- None --'),
    '#options' => [],
  );
  $entityType = $form_state
    ->getValue('entityType');
  if ($entityType) {
    $bundleType = $this->entityTypeManager
      ->getDefinition($entityType)
      ->getBundleEntityType();
    if ($bundleType) {
      $bundles = $this->entityTypeManager
        ->getStorage($bundleType)
        ->loadMultiple();
      if ($bundles) {
        foreach ($bundles as $bundle) {
          $form['entity']['entityBundle']['#options'][$bundle
            ->id()] = $bundle
            ->label();
        }
      }
      asort($form['entity']['entityBundle']['#options']);
    }
    $options = $this->entityDisplayRepository
      ->getViewModeOptions($entityType);
    if ($options) {
      asort($options);
      $form['entity']['entityDisplay']['#options'] += $options;
    }
  }
  $form['actions']['addNew'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Add'),
    '#button_type' => 'primary',
    '#submit' => [
      '::addDisplay',
    ],
    '#group' => 'display',
  );
  return $form;
}