You are here

function _menu_link_weight_get_options in Menu Link Weight 8

Same name and namespace in other branches
  1. 8.2 menu_link_weight.module \_menu_link_weight_get_options()
  2. 7 menu_link_weight.module \_menu_link_weight_get_options()

Gets a list of of options for a specific menu/parent.

Parameters

string $menu_name: The name of the menu.

string $parent_id: The parent link plugin ID.

int $current_mlid: The menu link for the current item.

string $new_item_title: The title for the new menu link to be created.

Return value

array List of options with index "link_current" or the menu link ID. Values include:

  • title: Santized title for the menu link.
  • weight: Calculated new weight.
  • db_weight: Current weight in the database, while form is being built.

See also

_menu_parents_recurse

2 calls to _menu_link_weight_get_options()
menu_link_weight_menu_link_content_element_process in ./menu_link_weight.menu_ui.inc
Process callback for the menu link weight element.
menu_link_weight_node_element_process in ./menu_link_weight.node.inc
Process callback for the menu link weight element.

File

./menu_link_weight.module, line 108
Replaces the menu link weight dropdown with a tabledrag widget.

Code

function _menu_link_weight_get_options($menu_name, $parent_id, $current_mlid, $new_item_title = NULL) {

  // Get the raw tree from the database.
  $tree = _menu_link_weight_get_tree($menu_name, $parent_id);

  // Weights will have to be re-ordered from -50 to 50 for fine-grained
  // control over the weight of the new element.
  $weight = MENU_LINK_WEIGHT_MIN_DELTA;
  $options = array();
  $link_current_title_build = [
    '#type' => 'inline_template',
    '#template' => '<strong><span class="menu-link-weight-link-current">{{ current_title }}</span></strong> ({% trans %}provided menu link{% endtrans %})',
    '#context' => [
      'current_title' => Unicode::truncate($new_item_title, 30, TRUE, FALSE),
    ],
  ];

  // Find out whether to add another (fake) item for the new link.
  $add_link = TRUE;
  foreach ($tree as $element) {
    if ($element->link
      ->getPluginId() === $current_mlid) {
      $add_link = FALSE;
    }
  }

  // Add link on top, if needed.
  if ($add_link) {
    $options['link_current'] = array(
      'title' => $link_current_title_build,
      'weight' => $weight,
      'db_weight' => NULL,
    );
    $weight++;
  }

  // Loop through the tree again.
  foreach ($tree as $element) {

    // Change the title & ID for the current menu link.
    $plugin_id = $element->link
      ->getPluginId();
    if ($plugin_id === $current_mlid) {
      $id = 'link_current';
      $title_build = $link_current_title_build;
    }
    else {
      $id = $plugin_id;
      $title = Unicode::truncate($element->link
        ->getTitle(), 30, TRUE, FALSE);
      $title_build = Link::fromTextAndUrl($title, $element->link
        ->getUrlObject())
        ->toRenderable();
      $element->link
        ->isEnabled();
      if (!$element->link
        ->isEnabled()) {
        $title_build['#suffix'] = ' (' . t('disabled') . ')';
      }
    }
    $options[$id] = array(
      'title' => $title_build,
      'weight' => $weight,
      'db_weight' => $element->link
        ->getWeight(),
    );
    $weight++;
  }
  return $options;
}