You are here

public function OgMenuInstanceForm::buildForm in Organic Groups Menu (OG Menu) 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/OgMenuInstanceForm.php, line 129

Class

OgMenuInstanceForm
Form controller for OG Menu instance edit forms.

Namespace

Drupal\og_menu\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  // On entity add, no links are attached yet, so bail out here.
  if ($this->entity
    ->isNew()) {
    return $form;
  }

  // Ensure that menu_overview_form_submit() knows the parents of this form
  // section.
  if (!$form_state
    ->has('menu_overview_form_parents')) {
    $form_state
      ->set('menu_overview_form_parents', []);
  }
  $form['#attached']['library'][] = 'menu_ui/drupal.menu_ui.adminforms';
  $tree = $this->menuTree
    ->load('ogmenu-' . $this->entity
    ->id(), new MenuTreeParameters());

  // We indicate that a menu administrator is running the menu access check.
  $this
    ->getRequest()->attributes
    ->set('_menu_admin', TRUE);
  $manipulators = array(
    array(
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ),
    array(
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ),
  );
  $tree = $this->menuTree
    ->transform($tree, $manipulators);
  $this
    ->getRequest()->attributes
    ->set('_menu_admin', FALSE);

  // Determine the delta; the number of weights to be made available.
  $count = function (array $tree) {
    $sum = function ($carry, MenuLinkTreeElement $item) {
      return $carry + $item
        ->count();
    };
    return array_reduce($tree, $sum);
  };
  $delta = max($count($tree), 50);
  $form['links'] = array(
    '#type' => 'table',
    '#theme' => 'table__menu_overview',
    '#header' => array(
      $this
        ->t('Menu link'),
      array(
        'data' => $this
          ->t('Enabled'),
        'class' => array(
          'checkbox',
        ),
      ),
      $this
        ->t('Weight'),
      array(
        'data' => $this
          ->t('Operations'),
        'colspan' => 3,
      ),
    ),
    '#attributes' => array(
      'id' => 'menu-overview',
    ),
    '#tabledrag' => array(
      array(
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'menu-parent',
        'subgroup' => 'menu-parent',
        'source' => 'menu-id',
        'hidden' => TRUE,
        'limit' => \Drupal::menuTree()
          ->maxDepth() - 1,
      ),
      array(
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'menu-weight',
      ),
    ),
  );

  // Check if the user has the global permission to add new links to the menu
  // instance, or has this permission inside the group.
  $permission = 'add new links to og menu instance entities';
  $has_permission = $this
    ->currentUser()
    ->hasPermission($permission) || $this->ogAccess
    ->userAccess($this->entity
    ->getGroup(), $permission, $this
    ->currentUser(), FALSE, TRUE)
    ->isAllowed();

  // Supply the empty text.
  if ($has_permission) {
    $form['links']['#empty'] = $this
      ->t('There are no menu links yet. <a href=":url">Add link</a>.', [
      ':url' => $this->urlGenerator
        ->generateFromRoute('entity.ogmenu_instance.add_link', [
        'ogmenu_instance' => $this->entity
          ->id(),
      ], [
        'query' => [
          'destination' => $this->entity
            ->toUrl('edit-form')
            ->toString(),
        ],
      ]),
    ]);
  }
  else {
    $form['links']['#empty'] = $this
      ->t('There are no menu links yet.');
  }
  $links = $this
    ->buildOverviewTreeForm($tree, $delta);
  foreach (Element::children($links) as $id) {
    if (isset($links[$id]['#item'])) {
      $element = $links[$id];
      $form['links'][$id]['#item'] = $element['#item'];

      // TableDrag: Mark the table row as draggable.
      $form['links'][$id]['#attributes'] = $element['#attributes'];
      $form['links'][$id]['#attributes']['class'][] = 'draggable';

      // TableDrag: Sort the table row according to its existing/configured weight.
      $form['links'][$id]['#weight'] = $element['#item']->link
        ->getWeight();

      // Add special classes to be used for tabledrag.js.
      $element['parent']['#attributes']['class'] = array(
        'menu-parent',
      );
      $element['weight']['#attributes']['class'] = array(
        'menu-weight',
      );
      $element['id']['#attributes']['class'] = array(
        'menu-id',
      );
      $form['links'][$id]['title'] = array(
        array(
          '#theme' => 'indentation',
          '#size' => $element['#item']->depth - 1,
        ),
        $element['title'],
      );
      $form['links'][$id]['enabled'] = $element['enabled'];
      $form['links'][$id]['enabled']['#wrapper_attributes']['class'] = array(
        'checkbox',
        'menu-enabled',
      );
      $form['links'][$id]['weight'] = $element['weight'];

      // Operations (dropbutton) column.
      $form['links'][$id]['operations'] = $element['operations'];
      $form['links'][$id]['id'] = $element['id'];
      $form['links'][$id]['parent'] = $element['parent'];
    }
  }
  return $form;
}