class MenuLink

Defines the menu link entity class.

Plugin annotation


@EntityType(
  id = "menu_link",
  label = @Translation("Menu link"),
  module = "menu_link",
  controllers = {
    "storage" = "Drupal\menu_link\MenuLinkStorageController",
    "render" = "Drupal\Core\Entity\EntityRenderController",
    "form" = {
      "default" = "Drupal\menu_link\MenuLinkFormController"
    }
  },
  static_cache = FALSE,
  base_table = "menu_links",
  uri_callback = "menu_link_uri",
  entity_keys = {
    "id" = "mlid",
    "label" = "link_title",
    "uuid" = "uuid",
    "bundle" = "bundle"
  },
  bundle_keys = {
    "bundle" = "bundle"
  }
)

Hierarchy

Expanded class hierarchy of MenuLink

12 files declare their use of MenuLink
book.module in drupal/core/modules/book/book.module
Allows users to create and organize related content in an outline.
LinkDelete.php in drupal/core/modules/shortcut/lib/Drupal/shortcut/Form/LinkDelete.php
Contains \Drupal\shortcut\Form\LinkDelete.
menu.admin.inc in drupal/core/modules/menu/menu.admin.inc
Administrative page callbacks for menu module.
menu.inc in drupal/core/includes/menu.inc
API for the Drupal menu system.
menu.module in drupal/core/modules/menu/menu.module
Allows administrators to customize the site's navigation menus.

... See full list

File

drupal/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php, line 46
Contains \Drupal\menu_link\Plugin\Core\Entity\MenuLink.

Namespace

Drupal\menu_link\Plugin\Core\Entity
View source
class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface {

  /**
   * The link's menu name.
   *
   * @var string
   */
  public $menu_name = 'tools';

  /**
   * The link's bundle.
   *
   * @var string
   */
  public $bundle = 'tools';

  /**
   * The menu link ID.
   *
   * @var int
   */
  public $mlid;

  /**
   * The menu link UUID.
   *
   * @var string
   */
  public $uuid;

  /**
   * The parent link ID.
   *
   * @var int
   */
  public $plid;

  /**
   * The Drupal path or external path this link points to.
   *
   * @var string
   */
  public $link_path;

  /**
   * For links corresponding to a Drupal path (external = 0), this connects the
   * link to a {menu_router}.path for joins.
   *
   * @var string
   */
  public $router_path;

  /**
   * The entity label.
   *
   * @var string
   */
  public $link_title = '';

  /**
   * A serialized array of options to be passed to the url() or l() function,
   * such as a query string or HTML attributes.
   *
   * @var array
   */
  public $options = array();

  /**
   * The name of the module that generated this link.
   *
   * @var string
   */
  public $module = 'menu';

  /**
   * A flag for whether the link should be rendered in menus.
   *
   * @var int
   */
  public $hidden = 0;

  /**
   * A flag to indicate if the link points to a full URL starting with a
   * protocol, like http:// (1 = external, 0 = internal).
   *
   * @var int
   */
  public $external;

  /**
   * Flag indicating whether any links have this link as a parent.
   *
   * @var int
   */
  public $has_children = 0;

  /**
   * Flag for whether this link should be rendered as expanded in menus.
   * Expanded links always have their child links displayed, instead of only
   * when the link is in the active trail.
   *
   * @var int
   */
  public $expanded = 0;

  /**
   * Link weight among links in the same menu at the same depth.
   *
   * @var int
   */
  public $weight = 0;

  /**
   * The depth relative to the top level. A link with plid == 0 will have
   * depth == 1.
   *
   * @var int
   */
  public $depth;

  /**
   * A flag to indicate that the user has manually created or edited the link.
   *
   * @var int
   */
  public $customized = 0;

  /**
   * The first entity ID in the materialized path.
   *
   * @var int
   *
   * @todo Investigate whether the p1, p2, .. pX properties can be moved to a
   * single array property.
   */
  public $p1;

  /**
   * The second entity ID in the materialized path.
   *
   * @var int
   */
  public $p2;

  /**
   * The third entity ID in the materialized path.
   *
   * @var int
   */
  public $p3;

  /**
   * The fourth entity ID in the materialized path.
   *
   * @var int
   */
  public $p4;

  /**
   * The fifth entity ID in the materialized path.
   *
   * @var int
   */
  public $p5;

  /**
   * The sixth entity ID in the materialized path.
   *
   * @var int
   */
  public $p6;

  /**
   * The seventh entity ID in the materialized path.
   *
   * @var int
   */
  public $p7;

  /**
   * The eighth entity ID in the materialized path.
   *
   * @var int
   */
  public $p8;

  /**
   * The ninth entity ID in the materialized path.
   *
   * @var int
   */
  public $p9;

  /**
   * The menu link modification timestamp.
   *
   * @var int
   */
  public $updated = 0;

  /**
   * The name of the route associated with this menu link, if any.
   *
   * @var string
   */
  public $route_name;

  /**
   * The route object associated with this menu link, if any.
   *
   * @var \Symfony\Component\Routing\Route
   */
  protected $routeObject;

  /**
   * Overrides Entity::id().
   */
  public function id() {
    return $this->mlid;
  }

  /**
   * {@inheritdoc}
   */
  public function bundle() {
    return $this->bundle;
  }

  /**
   * Overrides Entity::createDuplicate().
   */
  public function createDuplicate() {
    $duplicate = parent::createDuplicate();
    $duplicate->plid = NULL;
    return $duplicate;
  }

  /**
   * {@inheritdoc}
   */
  public function getRoute() {
    if (!$this->route_name) {
      return NULL;
    }
    if (!$this->routeObject instanceof Route) {
      $route_provider = drupal_container()
        ->get('router.route_provider');
      $this->routeObject = $route_provider
        ->getRouteByName($this->route_name);
    }
    return $this->routeObject;
  }

  /**
   * {@inheritdoc}
   */
  public function setRouteObject(Route $route) {
    $this->routeObject = $route;
  }

  /**
   * {@inheritdoc}
   */
  public function reset() {

    // To reset the link to its original values, we need to retrieve its
    // definition from hook_menu(). Otherwise, for example, the link's menu
    // would not be reset, because properties like the original 'menu_name' are
    // not stored anywhere else. Since resetting a link happens rarely and this
    // is a one-time operation, retrieving the full menu router does no harm.
    $menu = menu_get_router();
    $router_item = $menu[$this->router_path];
    $new_link = self::buildFromRouterItem($router_item);

    // Merge existing menu link's ID and 'has_children' property.
    foreach (array(
      'mlid',
      'has_children',
    ) as $key) {
      $new_link->{$key} = $this->{$key};
    }
    $new_link
      ->save();
    return $new_link;
  }

  /**
   * {@inheritdoc}
   */
  public static function buildFromRouterItem(array $item) {

    // Suggested items are disabled by default.
    if ($item['type'] == MENU_SUGGESTED_ITEM) {
      $item['hidden'] = 1;
    }
    elseif (!($item['type'] & MENU_VISIBLE_IN_TREE)) {
      $item['hidden'] = -1;
    }

    // Note, we set this as 'system', so that we can be sure to distinguish all
    // the menu links generated automatically from entries in {menu_router}.
    $item['module'] = 'system';
    $item += array(
      'link_title' => $item['title'],
      'link_path' => $item['path'],
      'options' => empty($item['description']) ? array() : array(
        'attributes' => array(
          'title' => $item['description'],
        ),
      ),
    );
    return \Drupal::entityManager()
      ->getStorageController('menu_link')
      ->create($item);
  }

  /**
   * Implements ArrayAccess::offsetExists().
   */
  public function offsetExists($offset) {
    return isset($this->{$offset});
  }

  /**
   * Implements ArrayAccess::offsetGet().
   */
  public function &offsetGet($offset) {
    return $this->{$offset};
  }

  /**
   * Implements ArrayAccess::offsetSet().
   */
  public function offsetSet($offset, $value) {
    $this->{$offset} = $value;
  }

  /**
   * Implements ArrayAccess::offsetUnset().
   */
  public function offsetUnset($offset) {
    unset($this->{$offset});
  }

}

Members

Name Modifiers Typesort descending Description Overrides
MenuLink::id public function Overrides Entity::id(). Overrides Entity::id
MenuLink::bundle public function Implements \Drupal\Core\Entity\EntityInterface::bundle(). Overrides Entity::bundle
MenuLink::createDuplicate public function Overrides Entity::createDuplicate(). Overrides Entity::createDuplicate
MenuLink::getRoute public function Returns the Route object associated with this link, if any. Overrides MenuLinkInterface::getRoute
MenuLink::setRouteObject public function Sets the route object for this link. Overrides MenuLinkInterface::setRouteObject
MenuLink::reset public function Resets a system-defined menu link. Overrides MenuLinkInterface::reset
MenuLink::buildFromRouterItem public static function Builds a menu link entity from a router item. Overrides MenuLinkInterface::buildFromRouterItem
MenuLink::offsetExists public function Implements ArrayAccess::offsetExists().
MenuLink::offsetGet public function Implements ArrayAccess::offsetGet().
MenuLink::offsetSet public function Implements ArrayAccess::offsetSet().
MenuLink::offsetUnset public function Implements ArrayAccess::offsetUnset().
Entity::__construct public function Constructs an Entity object. 2
Entity::uuid public function Implements \Drupal\Core\Entity\EntityInterface::uuid(). Overrides EntityInterface::uuid 1
Entity::isNew public function Implements \Drupal\Core\Entity\EntityInterface::isNew(). Overrides EntityInterface::isNew 1
Entity::isNewRevision public function Implements \Drupal\Core\Entity\EntityInterface::isNewRevision(). Overrides EntityInterface::isNewRevision
Entity::enforceIsNew public function Implements \Drupal\Core\Entity\EntityInterface::enforceIsNew(). Overrides EntityInterface::enforceIsNew
Entity::setNewRevision public function Implements \Drupal\Core\Entity\EntityInterface::setNewRevision(). Overrides EntityInterface::setNewRevision
Entity::entityType public function Implements \Drupal\Core\Entity\EntityInterface::entityType(). Overrides EntityInterface::entityType
Entity::label public function Implements \Drupal\Core\Entity\EntityInterface::label(). Overrides EntityInterface::label 4
Entity::uri public function Implements \Drupal\Core\Entity\EntityInterface::uri(). Overrides EntityInterface::uri 8
Entity::uriRelationships public function Returns a list of URI relationships supported by this entity. Overrides EntityInterface::uriRelationships
Entity::get public function Implements \Drupal\Core\Entity\EntityInterface::get(). Overrides ComplexDataInterface::get 2
Entity::set public function Implements \Drupal\Core\TypedData\ComplexDataInterface::set(). Overrides ComplexDataInterface::set 2
Entity::getProperties public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getProperties(). Overrides ComplexDataInterface::getProperties 1
Entity::getPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyValues(). Overrides ComplexDataInterface::getPropertyValues 1
Entity::setPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::setPropertyValues(). Overrides ComplexDataInterface::setPropertyValues 1
Entity::getPropertyDefinition public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinition(). Overrides ComplexDataInterface::getPropertyDefinition 1
Entity::getPropertyDefinitions public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions(). Overrides ComplexDataInterface::getPropertyDefinitions 1
Entity::isEmpty public function Implements \Drupal\Core\TypedData\ComplexDataInterface::isEmpty(). Overrides ComplexDataInterface::isEmpty 1
Entity::getIterator public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getIterator(). 1
Entity::access public function Implements \Drupal\Core\TypedData\AccessibleInterface::access(). Overrides AccessibleInterface::access
Entity::language public function Implements \Drupal\Core\TypedData\TranslatableInterface::language(). Overrides TranslatableInterface::language 1
Entity::getTranslation public function Implements \Drupal\Core\TypedData\TranslatableInterface::getTranslation(). Overrides TranslatableInterface::getTranslation 1
Entity::translations public function Returns the languages the entity is translated to. 1
Entity::getTranslationLanguages public function Implements \Drupal\Core\TypedData\TranslatableInterface::getTranslationLanguages(). Overrides TranslatableInterface::getTranslationLanguages 1
Entity::save public function Implements \Drupal\Core\Entity\EntityInterface::save(). Overrides EntityInterface::save 6
Entity::delete public function Implements \Drupal\Core\Entity\EntityInterface::delete(). Overrides EntityInterface::delete 3
Entity::entityInfo public function Implements \Drupal\Core\Entity\EntityInterface::entityInfo(). Overrides EntityInterface::entityInfo
Entity::getRevisionId public function Implements \Drupal\Core\Entity\EntityInterface::getRevisionId(). Overrides EntityInterface::getRevisionId 4
Entity::isDefaultRevision public function Implements \Drupal\Core\Entity\EntityInterface::isDefaultRevision(). Overrides EntityInterface::isDefaultRevision 1
Entity::getExportProperties public function Implements \Drupal\Core\Entity\EntityInterface::getExportProperties(). Overrides EntityInterface::getExportProperties 1
Entity::getBCEntity public function Implements \Drupal\Core\Entity\EntityInterface::getBCEntity(). Overrides EntityInterface::getBCEntity 1
Entity::getNGEntity public function Implements \Drupal\Core\Entity\EntityInterface::getNGEntity(). Overrides EntityInterface::getNGEntity
Entity::getType public function Implements \Drupal\Core\TypedData\TypedDataInterface::getType(). Overrides TypedDataInterface::getType 2
Entity::getDefinition public function Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
Entity::getValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::getValue(). Overrides TypedDataInterface::getValue
Entity::setValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::setValue(). Overrides TypedDataInterface::setValue
Entity::getString public function Implements \Drupal\Core\TypedData\TypedDataInterface::getString(). Overrides TypedDataInterface::getString
Entity::getConstraints public function Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints(). Overrides TypedDataInterface::getConstraints
Entity::validate public function Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). Overrides TypedDataInterface::validate 1
Entity::onChange public function Implements \Drupal\Core\TypedData\ComplexDataInterface::onChange(). Overrides ComplexDataInterface::onChange
Entity::getName public function Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). Overrides TypedDataInterface::getName
Entity::getRoot public function Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). Overrides TypedDataInterface::getRoot
Entity::getPropertyPath public function Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). Overrides TypedDataInterface::getPropertyPath
Entity::getParent public function Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). Overrides TypedDataInterface::getParent
Entity::setContext public function Implements \Drupal\Core\TypedData\TypedDataInterface::setContext(). Overrides TypedDataInterface::setContext
Entity::isTranslatable public function Implements \Drupal\Core\Entity\EntityInterface::isTranslatable(). Overrides EntityInterface::isTranslatable
MenuLink::$menu_name public property The link's menu name.
MenuLink::$bundle public property The link's bundle.
MenuLink::$mlid public property The menu link ID.
MenuLink::$uuid public property The menu link UUID.
MenuLink::$plid public property The parent link ID.
MenuLink::$link_path public property The Drupal path or external path this link points to.
MenuLink::$router_path public property For links corresponding to a Drupal path (external = 0), this connects the link to a {menu_router}.path for joins.
MenuLink::$link_title public property The entity label.
MenuLink::$options public property A serialized array of options to be passed to the url() or l() function, such as a query string or HTML attributes.
MenuLink::$module public property The name of the module that generated this link.
MenuLink::$hidden public property A flag for whether the link should be rendered in menus.
MenuLink::$external public property A flag to indicate if the link points to a full URL starting with a protocol, like http:// (1 = external, 0 = internal).
MenuLink::$has_children public property Flag indicating whether any links have this link as a parent.
MenuLink::$expanded public property Flag for whether this link should be rendered as expanded in menus. Expanded links always have their child links displayed, instead of only when the link is in the active trail.
MenuLink::$weight public property Link weight among links in the same menu at the same depth.
MenuLink::$depth public property The depth relative to the top level. A link with plid == 0 will have depth == 1.
MenuLink::$customized public property A flag to indicate that the user has manually created or edited the link.
MenuLink::$p1 public property The first entity ID in the materialized path.
MenuLink::$p2 public property The second entity ID in the materialized path.
MenuLink::$p3 public property The third entity ID in the materialized path.
MenuLink::$p4 public property The fourth entity ID in the materialized path.
MenuLink::$p5 public property The fifth entity ID in the materialized path.
MenuLink::$p6 public property The sixth entity ID in the materialized path.
MenuLink::$p7 public property The seventh entity ID in the materialized path.
MenuLink::$p8 public property The eighth entity ID in the materialized path.
MenuLink::$p9 public property The ninth entity ID in the materialized path.
MenuLink::$updated public property The menu link modification timestamp.
MenuLink::$route_name public property The name of the route associated with this menu link, if any.
MenuLink::$routeObject protected property The route object associated with this menu link, if any.
Entity::$langcode public property The language code of the entity's default language. 7
Entity::$entityType protected property The entity type.
Entity::$enforceIsNew protected property Boolean indicating whether the entity should be forced to be new.
Entity::$newRevision protected property Boolean indicating whether a new revision should be created on save.
Entity::$isDefaultRevision protected property Indicates whether this is the default revision. 1