You are here

public function GroupContentMenuForm::buildForm in Group Content 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/GroupContentMenuForm.php, line 74

Class

GroupContentMenuForm
Form controller for Group menu instance edit forms.

Namespace

Drupal\group_content_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;
  }
  $group = $form_state
    ->get('group');
  if (!$group) {

    /** @var \Drupal\group\Entity\GroupContentInterface[] $group_contents */
    $group_contents = $this->entityTypeManager
      ->getStorage('group_content')
      ->loadByEntity($this->entity);

    // If no related group content, nothing to do. Bail early.
    if (!$group_contents) {
      return $form;
    }
    $group_content = reset($group_contents);
    $group = $group_content
      ->getGroup();
  }
  $form_state
    ->set('group', $group);

  // 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(GroupContentMenuInterface::MENU_PREFIX . $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 = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      '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 = static function (array $tree) {
    $sum = static function ($carry, MenuLinkTreeElement $item) {
      return $carry + $item
        ->count();
    };
    return array_reduce($tree, $sum);
  };
  $delta = max($count($tree), 50);
  $form['links'] = [
    '#type' => 'table',
    '#theme' => 'table__menu_overview',
    '#header' => [
      $this
        ->t('Menu link'),
      [
        'data' => $this
          ->t('Enabled'),
        'class' => [
          'checkbox',
        ],
      ],
      $this
        ->t('Weight'),
      [
        'data' => $this
          ->t('Operations'),
        'colspan' => 3,
      ],
    ],
    '#attributes' => [
      'id' => 'menu-overview',
    ],
    '#tabledrag' => [
      [
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'menu-parent',
        'subgroup' => 'menu-parent',
        'source' => 'menu-id',
        'hidden' => TRUE,
        'limit' => $this->menuTree
          ->maxDepth() - 1,
      ],
      [
        '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 = 'administer group content menu types';
  $plugin_id = 'group_content_menu:' . $this->entity
    ->getEntityTypeId();
  $has_permission = $this
    ->currentUser()
    ->hasPermission($permission) || $group
    ->hasPermission("view {$plugin_id} entity", $this
    ->currentUser());

  // 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' => Url::fromRoute('entity.group_content_menu.add_link', [
        'group' => $group
          ->id(),
        'group_content_menu' => $this->entity
          ->id(),
      ], [
        'query' => [
          'destination' => $this->entity
            ->toUrl('edit-form')
            ->toString(),
        ],
      ])
        ->toString(),
    ]);
  }
  else {
    $form['links']['#empty'] = $this
      ->t('There are no menu links yet.');
  }
  $links = $this
    ->buildOverviewTreeForm($tree, $delta, $group);
  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'] = [
        'menu-parent',
      ];
      $element['weight']['#attributes']['class'] = [
        'menu-weight',
      ];
      $element['id']['#attributes']['class'] = [
        'menu-id',
      ];
      $form['links'][$id]['title'] = [
        [
          '#theme' => 'indentation',
          '#size' => $element['#item']->depth - 1,
        ],
        $element['title'],
      ];
      $form['links'][$id]['enabled'] = $element['enabled'];
      $form['links'][$id]['enabled']['#wrapper_attributes']['class'] = [
        '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;
}