You are here

public function BlocktabsEditForm::form in Block Tabs 8

Gets the actual form array to be built.

Overrides BlocktabsFormBase::form

See also

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

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

File

src/Form/BlocktabsEditForm.php, line 50

Class

BlocktabsEditForm
Controller for blocktabs edit form.

Namespace

Drupal\blocktabs\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $user_input = $form_state
    ->getUserInput();
  $form['#title'] = $this
    ->t('Edit blocktabs %name', [
    '%name' => $this->entity
      ->label(),
  ]);
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'blocktabs/admin';

  // Build the list of existing tabs for this blocktabs.
  $form['tabs'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Tab'),
      $this
        ->t('Weight'),
      $this
        ->t('Operations'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'tab-order-weight',
      ],
    ],
    '#attributes' => [
      'id' => 'blocktabs-tabs',
    ],
    '#empty' => $this
      ->t('There are currently no tabs in this blocktabs. Add one by selecting an option below.'),
    // Render tabs below parent elements.
    '#weight' => 5,
  ];
  foreach ($this->entity
    ->getTabs() as $tab) {
    $key = $tab
      ->getUuid();
    $form['tabs'][$key]['#attributes']['class'][] = 'draggable';
    $form['tabs'][$key]['#weight'] = isset($user_input['tabs']) ? $user_input['tabs'][$key]['weight'] : NULL;
    $form['tabs'][$key]['tab'] = [
      '#tree' => FALSE,
      'data' => [
        'label' => [
          '#plain_text' => $tab
            ->label(),
        ],
      ],
    ];
    $summary = $tab
      ->getSummary();
    if (!empty($summary)) {
      $summary['#prefix'] = ' ';
      $form['tabs'][$key]['tab']['data']['summary'] = $summary;
    }
    $form['tabs'][$key]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $tab
          ->label(),
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $tab
        ->getWeight(),
      '#delta' => 50,
      '#attributes' => [
        'class' => [
          'tab-order-weight',
        ],
      ],
    ];
    $links = [];
    $is_configurable = $tab instanceof ConfigurableTabInterface;
    if ($is_configurable) {
      $links['edit'] = [
        'title' => $this
          ->t('Edit'),
        'url' => Url::fromRoute('blocktabs.tab_edit_form', [
          'blocktabs' => $this->entity
            ->id(),
          'tab' => $key,
        ]),
      ];
    }
    $links['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => Url::fromRoute('blocktabs.tab_delete', [
        'blocktabs' => $this->entity
          ->id(),
        'tab' => $key,
      ]),
    ];
    $form['tabs'][$key]['operations'] = [
      '#type' => 'operations',
      '#links' => $links,
    ];
  }

  // Build the new tab addition form and add it to the tab list.
  $new_tab_options = [];
  $tabs = $this->tabManager
    ->getDefinitions();
  uasort($tabs, function ($a, $b) {
    return strcasecmp($a['id'], $b['id']);
  });
  foreach ($tabs as $tab => $definition) {
    $new_tab_options[$tab] = $definition['label'];
  }
  $form['tabs']['new'] = [
    '#tree' => FALSE,
    '#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
    '#attributes' => [
      'class' => [
        'draggable',
      ],
    ],
  ];
  $form['tabs']['new']['tab'] = [
    'data' => [
      'new' => [
        '#type' => 'select',
        '#title' => $this
          ->t('Tab'),
        '#title_display' => 'invisible',
        '#options' => $new_tab_options,
        '#empty_option' => $this
          ->t('Select a new tab'),
      ],
      [
        'add' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Add'),
          '#validate' => [
            '::tabValidate',
          ],
          '#submit' => [
            '::submitForm',
            '::tabSave',
          ],
        ],
      ],
    ],
    '#prefix' => '<div class="blocktabs-new">',
    '#suffix' => '</div>',
  ];
  $form['tabs']['new']['weight'] = [
    '#type' => 'weight',
    '#title' => $this
      ->t('Weight for new tab'),
    '#title_display' => 'invisible',
    '#default_value' => count($this->entity
      ->getTabs()) + 1,
    '#attributes' => [
      'class' => [
        'tab-order-weight',
      ],
    ],
  ];
  $form['tabs']['new']['operations'] = [
    'data' => [],
  ];
  return parent::form($form, $form_state);
}