You are here

protected function GroupContentMenuForm::buildOverviewTreeForm in Group Content Menu 8

Build overview tree form.

1 call to GroupContentMenuForm::buildOverviewTreeForm()
GroupContentMenuForm::buildForm in src/Form/GroupContentMenuForm.php
Form constructor.

File

src/Form/GroupContentMenuForm.php, line 224

Class

GroupContentMenuForm
Form controller for Group menu instance edit forms.

Namespace

Drupal\group_content_menu\Form

Code

protected function buildOverviewTreeForm($tree, $delta, GroupInterface $group) {
  $form =& $this->overviewTreeForm;
  $tree_access_cacheability = new CacheableMetadata();
  foreach ($tree as $element) {
    $tree_access_cacheability = $tree_access_cacheability
      ->merge(CacheableMetadata::createFromObject($element->access));

    // Only render accessible links.
    if (!$element->access
      ->isAllowed()) {
      continue;
    }

    /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
    $link = $element->link;
    if ($link) {
      $id = 'menu_plugin_id:' . $link
        ->getPluginId();
      $form[$id]['#item'] = $element;
      $form[$id]['#attributes'] = $link
        ->isEnabled() ? [
        'class' => [
          'menu-enabled',
        ],
      ] : [
        'class' => [
          'menu-disabled',
        ],
      ];
      $form[$id]['title'] = Link::fromTextAndUrl($link
        ->getTitle(), $link
        ->getUrlObject())
        ->toRenderable();
      if (!$link
        ->isEnabled()) {
        $form[$id]['title']['#suffix'] = ' (' . $this
          ->t('disabled') . ')';
      }
      elseif ($id === 'menu_plugin_id:user.logout') {
        $form[$id]['title']['#suffix'] = ' (' . $this
          ->t('<q>Log in</q> for anonymous users') . ')';
      }
      elseif (($url = $link
        ->getUrlObject()) && $url
        ->isRouted() && $url
        ->getRouteName() == 'user.page') {
        $form[$id]['title']['#suffix'] = ' (' . $this
          ->t('logged in users only') . ')';
      }
      $form[$id]['enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable @title menu link', [
          '@title' => $link
            ->getTitle(),
        ]),
        '#title_display' => 'invisible',
        '#default_value' => $link
          ->isEnabled(),
      ];
      $form[$id]['weight'] = [
        '#type' => 'weight',
        '#delta' => $delta,
        '#default_value' => $link
          ->getWeight(),
        '#title' => $this
          ->t('Weight for @title', [
          '@title' => $link
            ->getTitle(),
        ]),
        '#title_display' => 'invisible',
      ];
      $form[$id]['id'] = [
        '#type' => 'hidden',
        '#value' => $link
          ->getPluginId(),
      ];
      $form[$id]['parent'] = [
        '#type' => 'hidden',
        '#default_value' => $link
          ->getParent(),
      ];

      // Build a list of operations.
      $operations = [];
      $operations['edit'] = [
        'title' => $this
          ->t('Edit'),
      ];

      // Use this module's edit route for the menu. This means we don't have
      // to give elevated menu_ui access to edit menu links.
      $operations['edit']['url'] = Url::fromRoute('entity.group_content_menu.edit_link', [
        'group' => $group
          ->id(),
        'group_content_menu' => $this->entity
          ->id(),
        'menu_link_content' => $link
          ->getMetaData()['entity_id'],
      ]);

      // Bring the user back to the menu overview.
      $operations['edit']['query'] = [
        'destination' => Url::fromRouteMatch($this
          ->getRouteMatch())
          ->toString(),
      ];

      // Links can either be reset or deleted, not both.
      if ($link
        ->isResettable()) {
        $operations['reset'] = [
          'title' => $this
            ->t('Reset'),
          'url' => Url::fromRoute('menu_ui.link_reset', [
            'menu_link_plugin' => $link
              ->getPluginId(),
          ]),
        ];
      }
      elseif ($delete_link = $link
        ->getDeleteRoute()) {
        $operations['delete']['url'] = $delete_link;
        $operations['delete']['query'] = $this
          ->getRedirectDestination()
          ->getAsArray();
        $operations['delete']['title'] = $this
          ->t('Delete');
      }
      if ($link
        ->isTranslatable()) {
        $operations['translate'] = [
          'title' => $this
            ->t('Translate'),
          'url' => $link
            ->getTranslateRoute(),
        ];
        $operations['translate']['query'] = [
          'destination' => Url::fromRouteMatch($this
            ->getRouteMatch())
            ->toString(),
        ];
      }

      // Only display the operations to which the user has access.
      foreach ($operations as $key => $operation) {
        if (!$operation['url']
          ->access()) {
          unset($operations[$key]);
        }
      }
      $form[$id]['operations'] = [
        '#type' => 'operations',
        '#links' => $operations,
      ];
    }
    if ($element->subtree) {
      $this
        ->buildOverviewTreeForm($element->subtree, $delta, $group);
    }
  }
  $tree_access_cacheability
    ->merge(CacheableMetadata::createFromRenderArray($form))
    ->applyTo($form);
  return $form;
}