diff --git a/core/includes/menu.inc b/core/includes/menu.inc index cd8121d..db7fb77 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -2883,14 +2883,14 @@ function _menu_navigation_links_rebuild($menu) { // Updated and customized items whose router paths are gone need new ones. $menu_links = $menu_link_controller->loadUpdatedCustomized($paths); foreach ($menu_links as $menu_link) { - $router_path = _menu_find_router_path($menu_link->link_path); - if (!empty($router_path) && ($router_path != $menu_link->router_path || $menu_link->updated)) { + $router_path = _menu_find_router_path($menu_link->link_path->value); + if (!empty($router_path) && ($router_path != $menu_link->router_path->value || $menu_link->updated->value)) { // If the router path and the link path matches, it's surely a working // item, so we clear the updated flag. - $updated = $menu_link->updated && $router_path != $menu_link->link_path; + $updated = $menu_link->updated->value && $router_path != $menu_link->link_path->value; - $menu_link->router_path = $router_path; - $menu_link->updated = (int) $updated; + $menu_link->router_path->value = $router_path; + $menu_link->updated->value = (int) $updated; $menu_link_controller->save($menu_link); } } diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php index 9aa76d4..be1c57f 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php @@ -7,7 +7,7 @@ namespace Drupal\menu_link; -use Drupal\Core\Entity\EntityFormController; +use Drupal\Core\Entity\EntityFormControllerNG; use Drupal\Core\Language\Language; use Drupal\Core\Entity\EntityControllerInterface; use Drupal\Core\Path\AliasManagerInterface; @@ -16,7 +16,7 @@ /** * Form controller for the node edit forms. */ -class MenuLinkFormController extends EntityFormController implements EntityControllerInterface { +class MenuLinkFormController extends EntityFormControllerNG implements EntityControllerInterface { /** * The path alias manager. @@ -61,37 +61,37 @@ public function form(array $form, array &$form_state) { if (!$menu_link->isNew()) { // Get the human-readable menu title from the given menu name. $titles = menu_get_menus(); - $current_title = $titles[$menu_link->menu_name]; + $current_title = $titles[$menu_link->menu_name->value]; // Get the current breadcrumb and add a link to that menu's overview page. $breadcrumb = menu_get_active_breadcrumb(); - $breadcrumb[] = l($current_title, 'admin/structure/menu/manage/' . $menu_link->menu_name); + $breadcrumb[] = l($current_title, 'admin/structure/menu/manage/' . $menu_link->menu_name->value); drupal_set_breadcrumb($breadcrumb); } $form['link_title'] = array( '#type' => 'textfield', '#title' => t('Menu link title'), - '#default_value' => $menu_link->link_title, + '#default_value' => $menu_link->link_title->value, '#description' => t('The text to be used for this link in the menu.'), '#required' => TRUE, ); foreach (array('link_path', 'mlid', 'module', 'has_children', 'options') as $key) { - $form[$key] = array('#type' => 'value', '#value' => $menu_link->{$key}); + $form[$key] = array('#type' => 'value', '#value' => $menu_link->{$key}->value); } // Any item created or edited via this interface is considered "customized". $form['customized'] = array('#type' => 'value', '#value' => 1); // We are not using url() when constructing this path because it would add // $base_path. - $path = $menu_link->link_path; - if (isset($menu_link->options['query'])) { - $path .= '?' . drupal_http_build_query($menu_link->options['query']); + $path = $menu_link->link_path->value; + if (isset($menu_link->options->value['query'])) { + $path .= '?' . drupal_http_build_query($menu_link->options->value['query']); } - if (isset($menu_link->options['fragment'])) { - $path .= '#' . $menu_link->options['fragment']; + if (isset($menu_link->options->value['fragment'])) { + $path .= '#' . $menu_link->options->value['fragment']; } - if ($menu_link->module == 'menu') { + if ($menu_link->module->value == 'menu') { $form['link_path'] = array( '#type' => 'textfield', '#title' => t('Path'), @@ -105,33 +105,33 @@ public function form(array $form, array &$form_state) { $form['_path'] = array( '#type' => 'item', '#title' => t('Path'), - '#description' => l($menu_link->link_title, $menu_link->href, $menu_link->options), + '#description' => l($menu_link->link_title->value, $menu_link->href->value, $menu_link->options->value), ); } $form['description'] = array( '#type' => 'textarea', '#title' => t('Description'), - '#default_value' => isset($menu_link->options['attributes']['title']) ? $menu_link->options['attributes']['title'] : '', + '#default_value' => isset($menu_link->options->value['attributes']['title']) ? $menu_link->options->value['attributes']['title'] : '', '#rows' => 1, '#description' => t('Shown when hovering over the menu link.'), ); $form['enabled'] = array( '#type' => 'checkbox', '#title' => t('Enabled'), - '#default_value' => !$menu_link->hidden, + '#default_value' => !$menu_link->hidden->value, '#description' => t('Menu links that are not enabled will not be listed in any menu.'), ); $form['expanded'] = array( '#type' => 'checkbox', '#title' => t('Show as expanded'), - '#default_value' => $menu_link->expanded, + '#default_value' => $menu_link->expanded->value, '#description' => t('If selected and this menu link has children, the menu will always appear expanded.'), ); // Generate a list of possible parents (not including this link or descendants). $options = menu_parent_options(menu_get_menus(), $menu_link); - $default = $menu_link->menu_name . ':' . $menu_link->plid; + $default = $menu_link->menu_name->value . ':' . $menu_link->plid->target_id; if (!isset($options[$default])) { $default = 'tools:0'; } @@ -146,13 +146,13 @@ public function form(array $form, array &$form_state) { // Get number of items in menu so the weight selector is sized appropriately. $delta = \Drupal::entityManager() - ->getStorageController('menu_link')->countMenuLinks($menu_link->menu_name); + ->getStorageController('menu_link')->countMenuLinks($menu_link->menu_name->value); $form['weight'] = array( '#type' => 'weight', '#title' => t('Weight'), // Old hardcoded value. '#delta' => max($delta, 50), - '#default_value' => $menu_link->weight, + '#default_value' => $menu_link->weight->value, '#description' => t('Optional. In the menu, the heavier links will sink and the lighter links will be positioned nearer the top.'), ); @@ -160,7 +160,7 @@ public function form(array $form, array &$form_state) { '#type' => 'language_select', '#title' => t('Language'), '#languages' => Language::STATE_ALL, - '#default_value' => $menu_link->langcode, + '#default_value' => $menu_link->langcode->value, ); return parent::form($form, $form_state, $menu_link); @@ -172,44 +172,44 @@ public function form(array $form, array &$form_state) { protected function actions(array $form, array &$form_state) { $element = parent::actions($form, $form_state); $element['submit']['#button_type'] = 'primary'; - $element['delete']['#access'] = $this->entity->module == 'menu'; + $element['delete']['#access'] = $this->entity->module->value == 'menu'; return $element; } /** - * Overrides EntityFormController::validate(). + * {@inheritdoc} */ public function validate(array $form, array &$form_state) { $menu_link = $this->buildEntity($form, $form_state); - $normal_path = $this->pathAliasManager->getSystemPath($menu_link->link_path); - if ($menu_link->link_path != $normal_path) { + $normal_path = $this->pathAliasManager->getSystemPath($menu_link->link_path->value); + if ($menu_link->link_path->value != $normal_path) { drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $menu_link->link_path, '%normal_path' => $normal_path))); - $menu_link->link_path = $normal_path; + $menu_link->link_path->value = $normal_path; } - if (!url_is_external($menu_link->link_path)) { - $parsed_link = parse_url($menu_link->link_path); + if (!url_is_external($menu_link->link_path->value)) { + $parsed_link = parse_url($menu_link->link_path->value); if (isset($parsed_link['query'])) { - $menu_link->options['query'] = drupal_get_query_array($parsed_link['query']); + $menu_link->options->value['query'] = drupal_get_query_array($parsed_link['query']); } else { // Use unset() rather than setting to empty string // to avoid redundant serialized data being stored. - unset($menu_link->options['query']); + unset($menu_link->options->value['query']); } if (isset($parsed_link['fragment'])) { - $menu_link->options['fragment'] = $parsed_link['fragment']; + $menu_link->options->value['fragment'] = $parsed_link['fragment']; } else { - unset($menu_link->options['fragment']); + unset($menu_link->options->value['fragment']); } - if (isset($parsed_link['path']) && $menu_link->link_path != $parsed_link['path']) { - $menu_link->link_path = $parsed_link['path']; + if (isset($parsed_link['path']) && $menu_link->link_path->value != $parsed_link['path']) { + $menu_link->link_path->value = $parsed_link['path']; } } - if (!trim($menu_link->link_path) || !drupal_valid_path($menu_link->link_path, TRUE)) { - form_set_error('link_path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $menu_link->link_path))); + if (!trim($menu_link->link_path->value) || !drupal_valid_path($menu_link->link_path->value, TRUE)) { + form_set_error('link_path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $menu_link->link_path->value))); } parent::validate($form, $form_state); @@ -224,11 +224,12 @@ public function submit(array $form, array &$form_state) { // The value of "hidden" is the opposite of the value supplied by the // "enabled" checkbox. - $menu_link->hidden = (int) !$menu_link->enabled; - unset($menu_link->enabled); + $menu_link->hidden->value = (int) !$menu_link->enabled->value; + // @todo Check out this 'enabled' stuff. +// unset($menu_link->enabled); - $menu_link->options['attributes']['title'] = $menu_link->description; - list($menu_link->menu_name, $menu_link->plid) = explode(':', $menu_link->parent); + $menu_link->options->value['attributes']['title'] = $menu_link->description->value; + list($menu_link->menu_name->value, $menu_link->plid->target_id) = explode(':', $menu_link->parent); return $menu_link; } @@ -237,13 +238,9 @@ public function submit(array $form, array &$form_state) { * Overrides EntityFormController::save(). */ public function save(array $form, array &$form_state) { - $menu_link = $this->entity; - - $saved = $menu_link->save(); - - if ($saved) { + if ($this->entity->save()) { drupal_set_message(t('The menu link has been saved.')); - $form_state['redirect'] = 'admin/structure/menu/manage/' . $menu_link->menu_name; + $form_state['redirect'] = 'admin/structure/menu/manage/' . $this->entity->menu_name->value; } else { drupal_set_message(t('There was an error saving the menu link.'), 'error'); @@ -255,7 +252,6 @@ public function save(array $form, array &$form_state) { * Overrides EntityFormController::delete(). */ public function delete(array $form, array &$form_state) { - $menu_link = $this->entity; - $form_state['redirect'] = 'admin/structure/menu/item/' . $menu_link->id() . '/delete'; + $form_state['redirect'] = 'admin/structure/menu/item/' . $this->entity->id() . '/delete'; } } diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php index 6a3eddb..2366bea 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php @@ -7,7 +7,7 @@ namespace Drupal\menu_link; -use Drupal\Core\Entity\DatabaseStorageController; +use Drupal\Core\Entity\DatabaseStorageControllerNG; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Database\Connection; @@ -16,12 +16,9 @@ use Symfony\Component\HttpFoundation\Request; /** - * Controller class for menu links. - * - * This extends the Drupal\entity\DatabaseStorageController class, adding - * required special handling for menu_link entities. + * Defines the storage controller class for menu links. */ -class MenuLinkStorageController extends DatabaseStorageController { +class MenuLinkStorageController extends DatabaseStorageControllerNG { /** * Indicates whether the delete operation should re-parent children items. @@ -45,16 +42,7 @@ class MenuLinkStorageController extends DatabaseStorageController { protected $routeProvider; /** - * Overrides DatabaseStorageController::__construct(). - * - * @param string $entity_type - * The entity type for which the instance is created. - * @param array $entity_info - * An array of entity info for the entity type. - * @param \Drupal\Core\Database\Connection $database - * The database connection to be used. - * @param \Symfony\Cmf\Component\Routing\RouteProviderInterface $route_provider - * The route provider service. + * {@inheritdoc} */ public function __construct($entity_type, array $entity_info, Connection $database, RouteProviderInterface $route_provider) { parent::__construct($entity_type, $entity_info, $database); @@ -79,7 +67,7 @@ public static function createInstance(ContainerInterface $container, $entity_typ } /** - * Overrides DatabaseStorageController::buildQuery(). + * {@inheritdoc} */ protected function buildQuery($ids, $revision_id = FALSE) { $query = parent::buildQuery($ids, $revision_id); @@ -90,27 +78,24 @@ protected function buildQuery($ids, $revision_id = FALSE) { } /** - * Overrides DatabaseStorageController::attachLoad(). - * - * @todo Don't call parent::attachLoad() at all because we want to be able to - * control the entity load hooks. + * {@inheritdoc} */ - protected function attachLoad(&$menu_links, $load_revision = FALSE) { + protected function attachLoad(&$queried_entities, $load_revision = FALSE) { $routes = array(); - foreach ($menu_links as &$menu_link) { - $menu_link->options = unserialize($menu_link->options); + foreach ($queried_entities as &$record) { + $record->options = unserialize($record->options); // Use the weight property from the menu link. - $menu_link->router_item['weight'] = $menu_link->weight; + $record->router_item['weight'] = $record->weight; // By default use the menu_name as type. - $menu_link->bundle = $menu_link->menu_name; + $record->bundle = $record->menu_name; // For all links that have an associated route, load the route object now // and save it on the object. That way we avoid a select N+1 problem later. - if ($menu_link->route_name) { - $routes[$menu_link->id()] = $menu_link->route_name; + if ($record->route_name) { + $routes[$record->{$this->idKey}] = $record->route_name; } } @@ -120,16 +105,18 @@ protected function attachLoad(&$menu_links, $load_revision = FALSE) { foreach ($routes as $entity_id => $route) { // Not all stored routes will be valid on load. if (isset($route_objects[$route])) { - $menu_links[$entity_id]->setRouteObject($route_objects[$route]); + $queried_entities[$entity_id]->setRouteObject($route_objects[$route]); } } } - parent::attachLoad($menu_links, $load_revision); + // @todo Consider not calling parent::attachLoad() at all if we want to be + // able to control the entity load hooks. + parent::attachLoad($queried_entities, $load_revision); } /** - * Overrides DatabaseStorageController::save(). + * {@inheritdoc} */ public function save(EntityInterface $entity) { // We return SAVED_UPDATED by default because the logic below might not @@ -194,65 +181,65 @@ public function save(EntityInterface $entity) { } /** - * Overrides DatabaseStorageController::preSave(). + * {@inheritdoc} */ protected function preSave(EntityInterface $entity) { - // This is the easiest way to handle the unique internal path '', - // since a path marked as external does not need to match a router path. - $entity->external = (url_is_external($entity->link_path) || $entity->link_path == '') ? 1 : 0; - - // Try to find a parent link. If found, assign it and derive its menu. - $parent_candidates = !empty($entity->parentCandidates) ? $entity->parentCandidates : array(); - $parent = $this->findParent($entity, $parent_candidates); - if ($parent) { - $entity->plid = $parent->id(); - $entity->menu_name = $parent->menu_name; - } - // If no corresponding parent link was found, move the link to the top-level. - else { - $entity->plid = 0; - } - - // Directly fill parents for top-level links. - if ($entity->plid == 0) { - $entity->p1 = $entity->id(); - for ($i = 2; $i <= MENU_MAX_DEPTH; $i++) { - $parent_property = "p$i"; - $entity->$parent_property = 0; - } - $entity->depth = 1; - } - // Otherwise, ensure that this link's depth is not beyond the maximum depth - // and fill parents based on the parent link. - else { - if ($entity->has_children && $entity->original) { - $limit = MENU_MAX_DEPTH - $this->findChildrenRelativeDepth($entity->original) - 1; - } - else { - $limit = MENU_MAX_DEPTH - 1; - } - if ($parent->depth > $limit) { - return FALSE; - } - $entity->depth = $parent->depth + 1; - $this->setParents($entity, $parent); - } - - // Need to check both plid and menu_name, since plid can be 0 in any menu. - if (isset($entity->original) && ($entity->plid != $entity->original->plid || $entity->menu_name != $entity->original->menu_name)) { - $this->moveChildren($entity, $entity->original); - } - // Find the router_path. - if (empty($entity->router_path) || empty($entity->original) || (isset($entity->original) && $entity->original->link_path != $entity->link_path)) { - if ($entity->external) { - $entity->router_path = ''; - } - else { - // Find the router path which will serve this path. - $entity->parts = explode('/', $entity->link_path, MENU_MAX_PARTS); - $entity->router_path = _menu_find_router_path($entity->link_path); - } - } + // This is the easiest way to handle the unique internal path '', + // since a path marked as external does not need to match a router path. + $entity->external->value = (url_is_external($entity->link_path->value) || $entity->link_path->value == '') ? 1 : 0; + + // Try to find a parent link. If found, assign it and derive its menu. + $parent_candidates = !empty($entity->parentCandidates) ? $entity->parentCandidates : array(); + $parent = $this->findParent($entity, $parent_candidates); + if ($parent) { + $entity->plid->target_id = $parent->id(); + $entity->menu_name->value = $parent->menu_name->value; + } + // If no corresponding parent link was found, move the link to the top-level. + else { + $entity->plid->target_id = 0; + } + + // Directly fill parents for top-level links. + if ($entity->plid->target_id == 0) { + $entity->p1->target_id = $entity->id(); + for ($i = 2; $i <= MENU_MAX_DEPTH; $i++) { + $parent_property = "p$i"; + $entity->$parent_property->target_id = 0; + } + $entity->depth->value = 1; + } + // Otherwise, ensure that this link's depth is not beyond the maximum depth + // and fill parents based on the parent link. + else { + if ($entity->has_children->value && $entity->original) { + $limit = MENU_MAX_DEPTH - $this->findChildrenRelativeDepth($entity->original) - 1; + } + else { + $limit = MENU_MAX_DEPTH - 1; + } + if ($parent->depth->value > $limit) { + return FALSE; + } + $entity->depth->value = $parent->depth->value + 1; + $this->setParents($entity, $parent); + } + + // Need to check both plid and menu_name, since plid can be 0 in any menu. + if (isset($entity->original) && ($entity->plid->target_id != $entity->original->plid->target_id || $entity->menu_name->value != $entity->original->menu_name->value)) { + $this->moveChildren($entity, $entity->original); + } + // Find the router_path. + if (empty($entity->router_path->value) || empty($entity->original) || (isset($entity->original) && $entity->original->link_path->value != $entity->link_path->value)) { + if ($entity->external->value) { + $entity->router_path->value = ''; + } + else { + // Find the router path which will serve this path. + $entity->parts = explode('/', $entity->link_path, MENU_MAX_PARTS); + $entity->router_path->value = _menu_find_router_path($entity->link_path->value); + } + } // Find the route_name. if (!isset($entity->route_name)) { $entity->route_name = $this->findRouteName($entity->link_path); @@ -286,16 +273,16 @@ protected function findRouteName($link_path) { } /** - * DatabaseStorageController::postSave(). + * {@inheritdoc} */ function postSave(EntityInterface $entity, $update) { // Check the has_children status of the parent. $this->updateParentalStatus($entity); - menu_cache_clear($entity->menu_name); - if (isset($entity->original) && $entity->menu_name != $entity->original->menu_name) { - menu_cache_clear($entity->original->menu_name); - } + menu_cache_clear($entity->menu_name->value); + if (isset($entity->original) && $entity->menu_name->value != $entity->original->menu_name->value) { + menu_cache_clear($entity->original->menu_name->value); + } // Now clear the cache. _menu_clear_page_cache(); @@ -312,7 +299,7 @@ public function preventReparenting($value = FALSE) { } /** - * Overrides DatabaseStorageController::preDelete(). + * {@inheritdoc} */ protected function preDelete($entities) { // Nothing to do if we don't want to reparent children. @@ -322,10 +309,10 @@ protected function preDelete($entities) { foreach ($entities as $entity) { // Children get re-attached to the item's parent. - if ($entity->has_children) { - $children = $this->loadByProperties(array('plid' => $entity->plid)); + if ($entity->has_children->value) { + $children = $this->loadByProperties(array('plid' => $entity->plid->target_id)); foreach ($children as $child) { - $child->plid = $entity->plid; + $child->plid->target_id = $entity->plid->target_id; $this->save($child); } } @@ -333,7 +320,7 @@ protected function preDelete($entities) { } /** - * Overrides DatabaseStorageController::postDelete(). + * {@inheritdoc} */ protected function postDelete($entities) { $affected_menus = array(); @@ -344,11 +331,10 @@ protected function postDelete($entities) { } // Store all menu names for which we need to clear the cache. - if (!isset($affected_menus[$entity->menu_name])) { - $affected_menus[$entity->menu_name] = $entity->menu_name; - } + $affected_menus[] = $entity->menu_name->value; } + array_unique($affected_menus); foreach ($affected_menus as $menu_name) { menu_cache_clear($menu_name); } diff --git a/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php b/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php index 5af23d7..93bced9 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php @@ -7,14 +7,13 @@ namespace Drupal\menu_link\Plugin\Core\Entity; +use Drupal\Core\Annotation\Translation; +use Drupal\Core\Entity\Annotation\EntityType; +use Drupal\Core\Entity\EntityNG; +use Drupal\Core\Language\Language; use Drupal\menu_link\MenuLinkInterface; use Symfony\Component\Routing\Route; -use Drupal\Core\Entity\Annotation\EntityType; -use Drupal\Core\Annotation\Translation; -use Drupal\Core\Entity\ContentEntityInterface; -use Drupal\Core\Entity\Entity; - /** * Defines the menu link entity class. * @@ -43,14 +42,14 @@ * } * ) */ -class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface { +class MenuLink extends EntityNG implements \ArrayAccess, MenuLinkInterface { /** * The link's menu name. * - * @var string + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $menu_name = 'tools'; + public $menu_name; /** * The link's bundle. @@ -62,21 +61,21 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface { /** * The menu link ID. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $mlid; /** * The menu link UUID. * - * @var string + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $uuid; /** * The parent link ID. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $plid; @@ -91,89 +90,89 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface { * For links corresponding to a Drupal path (external = 0), this connects the * link to a {menu_router}.path for joins. * - * @var string + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $router_path; /** * The entity label. * - * @var string + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $link_title = ''; + 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 + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $options = array(); + public $options; /** * The name of the module that generated this link. * - * @var string + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $module = 'menu'; + public $module; /** * A flag for whether the link should be rendered in menus. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $hidden = 0; + public $hidden; /** * A flag to indicate if the link points to a full URL starting with a * protocol, like http:// (1 = external, 0 = internal). * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $external; /** * Flag indicating whether any links have this link as a parent. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $has_children = 0; + public $has_children; /** * 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 + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $expanded = 0; + public $expanded; /** * Link weight among links in the same menu at the same depth. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $weight = 0; + public $weight; /** * The depth relative to the top level. A link with plid == 0 will have * depth == 1. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $depth; /** * A flag to indicate that the user has manually created or edited the link. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $customized = 0; + public $customized; /** * The first entity ID in the materialized path. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface * * @todo Investigate whether the p1, p2, .. pX properties can be moved to a * single array property. @@ -183,74 +182,93 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface { /** * The second entity ID in the materialized path. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $p2; /** * The third entity ID in the materialized path. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $p3; /** * The fourth entity ID in the materialized path. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $p4; /** * The fifth entity ID in the materialized path. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $p5; /** * The sixth entity ID in the materialized path. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $p6; /** * The seventh entity ID in the materialized path. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $p7; /** * The eighth entity ID in the materialized path. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $p8; /** * The ninth entity ID in the materialized path. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $p9; /** * The menu link modification timestamp. * - * @var int + * @var \Drupal\Core\Entity\Field\FieldInterface */ - public $updated = 0; + public $updated; /** * The name of the route associated with this menu link, if any. * - * @var string + * @var \Drupal\Core\Entity\Field\FieldInterface */ public $route_name; /** + * Default values for the menu link. + * + * @var array + */ + protected $values = array( + 'langcode' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => Language::LANGCODE_NOT_SPECIFIED))), + 'menu_name' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => 'tools'))), + 'link_title' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => ''))), + 'options' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => array()))), + 'module' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => 'menu'))), + 'hidden' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => 0))), + 'has_children' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => 0))), + 'expanded' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => 0))), + 'weight' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => 0))), + 'customized' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => 0))), + 'updated' => array(Language::LANGCODE_DEFAULT => array(0 => array('value' => 0))), + ); + + /** * The route object associated with this menu link, if any. * * @var \Symfony\Component\Routing\Route @@ -258,10 +276,10 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface { protected $routeObject; /** - * Overrides Entity::id(). + * {@inheritdoc} */ public function id() { - return $this->mlid; + return $this->get('mlid')->offsetGet(0)->get('value'); } /** @@ -272,11 +290,46 @@ public function bundle() { } /** + * {@inheritdoc} + */ + protected function init() { + parent::init(); + // We unset all defined properties, so magic getters apply. + unset($this->menu_name); + unset($this->mlid); + unset($this->uuid); + unset($this->plid); + unset($this->link_path); + unset($this->router_path); + unset($this->link_title); + unset($this->options); + unset($this->module); + unset($this->hidden); + unset($this->external); + unset($this->has_children); + unset($this->expanded); + unset($this->weight); + unset($this->depth); + unset($this->customized); + unset($this->p1); + unset($this->p2); + unset($this->p3); + unset($this->p4); + unset($this->p5); + unset($this->p6); + unset($this->p7); + unset($this->p8); + unset($this->p9); + unset($this->updated); + unset($this->route_name); + } + + /** * Overrides Entity::createDuplicate(). */ public function createDuplicate() { $duplicate = parent::createDuplicate(); - $duplicate->plid = NULL; + $duplicate->get('plid')->offsetGet(0)->set('value', NULL); return $duplicate; } @@ -346,30 +399,34 @@ public static function buildFromRouterItem(array $item) { } /** - * Implements ArrayAccess::offsetExists(). + * {@inheritdoc} */ public function offsetExists($offset) { - return isset($this->{$offset}); + return $this->get($offset)->offsetGet(0)->__isset(); +// return isset($this->{$offset}); } /** - * Implements ArrayAccess::offsetGet(). + * {@inheritdoc} */ public function &offsetGet($offset) { - return $this->{$offset}; + return $this->get($offset)->offsetGet(0)->get('value'); +// return $this->{$offset}; } /** - * Implements ArrayAccess::offsetSet(). + * {@inheritdoc} */ public function offsetSet($offset, $value) { - $this->{$offset} = $value; + $this->get($offset)->offsetGet(0)->set('value', $value); +// $this->{$offset} = $value; } /** - * Implements ArrayAccess::offsetUnset(). + * {@inheritdoc} */ public function offsetUnset($offset) { - unset($this->{$offset}); + $this->get($offset)->offsetGet(0)->set('value', NULL); +// unset($this->{$offset}); } }