You are here

class MenuLinkConfiguration in Configuration Management 7.2

Hierarchy

Expanded class hierarchy of MenuLinkConfiguration

File

lib/Drupal/configuration/Config/MenuLinkConfiguration.php, line 13
Definition of Drupal\configuration\Config\FieldConfiguration.

Namespace

Drupal\configuration\Config
View source
class MenuLinkConfiguration extends Configuration {

  /**
   * Overrides Drupal\configuration\Config\Configuration::__construct().
   */
  public function __construct($identifier) {
    parent::__construct($identifier);
    $keys = array(
      'link_path',
      'link_title',
      'menu_name',
      'weight',
      'expanded',
      'options',
      'router_path',
      'parent_identifier',
      'hidden',
      'module',
    );
    $this
      ->setKeysToExport($keys);
  }

  /**
   * Overrides Drupal\configuration\Config\Configuration::isActive().
   */
  public static function isActive() {
    return module_exists('menu');
  }

  /**
   * Overrides Drupal\configuration\Config\Configuration::getComponentHumanName().
   */
  public static function getComponentHumanName($component, $plural = FALSE) {
    return $plural ? t('Menu links') : t('Menu link');
  }

  /**
   * Overrides Drupal\configuration\Config\Configuration::getComponent().
   */
  public function getComponent() {
    return 'menu_link';
  }

  /**
   * Overrides Drupal\configuration\Config\Configuration::supportedComponents().
   */
  public static function supportedComponents() {
    return array(
      'menu_link',
    );
  }

  /**
   * Helper function to retrive a menu link based on its identifier.
   *
   * @param string $identifier
   *   The identifier of the configuration.
   * @param boolean $reset
   *   If TRUE the cache of this function is flushed.
   *
   * @return
   *   A menu link object.
   */
  public static function getMenuLinkByIdenfifier($identifier, $reset = FALSE) {
    static $menu_links;
    if (!isset($menu_links) || $reset) {
      $res = db_select('menu_links', 'ml')
        ->fields('ml', array(
        'menu_name',
        'link_path',
        'mlid',
      ))
        ->execute()
        ->fetchAll();
      $menu_links = array();
      foreach ($res as $menu_link) {
        $id = sha1(str_replace('-', '_', $menu_link->menu_name) . ':' . $menu_link->link_path);
        $menu_links[$id] = $menu_link->mlid;
      }
    }
    if (!empty($menu_links[$identifier])) {
      return $menu_links[$identifier];
    }
  }

  /**
   * Overrides Drupal\configuration\Config\Configuration::getAllIdentifiers().
   */
  public static function getAllIdentifiers($component) {
    global $menu_admin;

    // Need to set this to TRUE in order to get menu links that the
    // current user may not have access to (i.e. user/login)
    $menu_admin = TRUE;

    // This is intentionally to get always the same number of menus for each
    // user that can manage configurations.
    global $user;
    $current_user = $user;

    // Run the next line as administrator.
    $user = user_load(1);
    $menu_links = menu_parent_options(menu_get_menus(), array(
      'mlid' => 0,
    ));

    // Back to the previous user.
    $user = $current_user;
    $options = array();
    foreach ($menu_links as $key => $name) {
      list($menu_name, $mlid) = explode(':', $key, 2);
      if ($mlid != 0) {
        $link = menu_link_load($mlid);
        $identifier = sha1(str_replace('-', '_', $link['menu_name']) . ':' . $link['link_path']);
        $options[$identifier] = "{$menu_name}: {$name}";
      }
    }
    $menu_admin = FALSE;
    return $options;
  }

  /**
   * Overrides Drupal\configuration\Config\Configuration::alterDependencies().
   */
  public static function alterDependencies(Configuration $config) {
    if ($config
      ->getComponent() == 'menu_link') {
      $mlid = static::getMenuLinkByIdenfifier($config
        ->getIdentifier());
      $menulink = menu_link_load($mlid);
      if (!empty($menulink['plid'])) {
        $parent_menulink = menu_link_load($menulink['plid']);
        if ($parent_menulink) {
          $identifier = sha1(str_replace('-', '_', $parent_menulink['menu_name']) . ':' . $parent_menulink['link_path']);
          $menulink_config = new MenuLinkConfiguration($identifier);
          $menulink_config
            ->build();
          $config
            ->addToDependencies($menulink_config);
        }
      }
    }
  }

  /**
   * Overrides Drupal\configuration\Config\Configuration::prepareBuild().
   */
  protected function prepareBuild() {
    $mlid = static::getMenuLinkByIdenfifier($this
      ->getIdentifier(), TRUE);
    $this->data = menu_link_load($mlid);
    $this->data['parent_identifier'] = NULL;
    if (!empty($this->data['plid'])) {
      $parent = db_select('menu_links', 'ml')
        ->fields('ml', array(
        'menu_name',
        'link_path',
        'mlid',
      ))
        ->condition('mlid', $this->data['plid'])
        ->execute()
        ->fetchObject();
      if (!empty($parent)) {
        $this->data['parent_identifier'] = sha1(str_replace('-', '_', $parent->menu_name) . ':' . $parent->link_path);
      }
    }
    return $this;
  }

  /**
   * Overrides Drupal\configuration\Config\Configuration::saveToActiveStore().
   */
  public function saveToActiveStore(ConfigIteratorSettings &$settings) {
    $data = $this
      ->getData();

    // Determine if the menu already exists.
    $data['mlid'] = static::getMenuLinkByIdenfifier($this
      ->getIdentifier());
    if (!empty($data['parent_identifier'])) {
      $data['plid'] = static::getMenuLinkByIdenfifier($this->data['parent_identifier'], TRUE);
    }
    menu_link_save($data);
    $settings
      ->addInfo('imported', $this
      ->getUniqueId());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Configuration::$broken protected property A boolean flag to indicate if the configuration object couldn't be loaded from it source.
Configuration::$built protected property A boolean flag to indicate if the configuration object was already populated from the ActiveStore, or from the DataStore.
Configuration::$context protected property The ConfigIteratorSettings instance used by iterate.
Configuration::$data protected property The data of this configuration.
Configuration::$dependencies protected property An array of configuration objects required to use this configuration.
Configuration::$hash protected property A hash that represent that sumarizes the configuration and can be used to copare configurations.
Configuration::$identifier protected property The identifier that identifies to the component, usually the machine name.
Configuration::$keys_to_export protected property An array of keys names to export. If the array is empty, all the keys of the configuration will be exported.
Configuration::$optional_configurations protected property An array of configuration objects that are parts of this configurations but are not required to use this configuration.
Configuration::$required_modules protected property The required modules to load this configuration.
Configuration::$storage protected property An object to save and load the data from a persistent medium.
Configuration::addToDependencies public function Add a new dependency for this configuration.
Configuration::addToModules public function Add a new dependency for this configuration.
Configuration::addToOptionalConfigurations public function Add a new child configuration for this configuration.
Configuration::build public function Build the configuration object based on the component name and in the identifier.
Configuration::buildHash public function Create a unique hash for this configuration based on the data, dependencies, optional configurations and modules required to use this configuration. Use getHash() after call this function.
Configuration::checkDependencies public function Returns TRUE if all the dependencies of this configurations are met. Returns FALSE if a module or a dependency is required by this configuration is not enabled.
Configuration::configFileExists public function Returns TRUE if the file that represents this configuration exists in the datastore.
Configuration::configForEntity public function Return TRUE if this is the configuration for an entity. 3
Configuration::discoverModules protected function Internal function to discover what modules are required for the current being proccessed configurations.
Configuration::export public function
Configuration::findDependencies public function Ask to each configuration handler to add its dependencies to the current configuration that is being exported. 2
Configuration::findRequiredModules public function Add the required modules to load this configuration. 10
Configuration::getAllIdentifiersCached public static function Cache wrapper for getAllIdentifiers().
Configuration::getAvailableModules protected static function Helper for retrieving info from system table.
Configuration::getData public function Return the data for this configuration.
Configuration::getDependencies public function Returns the list of dependencies of this configuration
Configuration::getDependentModules public static function Determine the status of the given module and of its dependencies.
Configuration::getFileName public function Returns the filename that contains the content of the current configuration.
Configuration::getHash public function Returns the hash of the configuration object.
Configuration::getIdentifier public function Returns the identifier of the configuration object.
Configuration::getKeysToExport public function Returns an array of keys names to export. If the array is empty, all the keys of the configuration will be exported.
Configuration::getModules public function Returns the name of the required_modules that provide this configuration.
Configuration::getOptionalConfigurations public function Returns the list of optional_configurations of this configuration
Configuration::getRequiredModules public function Returns a list of modules that are required to run this configuration.
Configuration::getStatus public function Return the current status of the configuration.
Configuration::getStorageInstance protected static function Returns a Storage Object ready to load or write configurations from the disk. 2
Configuration::getStorageSystem protected static function Returns a class with its namespace to save data to the disk. 2
Configuration::getUniqueId public function Returns an unique identifier for this configuration. Usually something like 'content_type.article' where content_type is the component of the configuration and 'article' is the identifier of the configuration for the given component.
Configuration::import public function Load a configuration from the DataStore and save it into the ActiveStore. This function is called from iterator().
Configuration::inSync constant A bit flag used to let us know if a configuration is the same in both the activestore and the datastore.
Configuration::isBroken public function Return TRUE if something went wrong with the load of the configuration.
Configuration::iterate public function This function will exectute a callback function over all the configurations objects that it process.
Configuration::loadFromActiveStore public function Load a configurations from the database.
Configuration::loadFromStorage public function Load the Configuration data from the disk.
Configuration::moduleInstalled constant A bit flag used to let us know if a module for the configuration is already installed.
Configuration::moduleMissing constant A bit flag used to let us know if a module for the configuration is not available to install in the site.
Configuration::moduleToInstall constant A bit flag used to let us know if a module for the configuration is disabled but can be enabled.
Configuration::notTracked constant A bit flag used to let us know if a configuration is not currently being tracked.
Configuration::overridden constant A bit flag used to let us know if a configuration was overridden as a result of changing the activestore directly. (config changes via the UI)
Configuration::printRaw protected function Print the configuration as plain text formatted to use in a tar file.
Configuration::raw public function Print the configuration as plain text formatted to use in a tar file.
Configuration::removeConfiguration public function Removes the configuration record from the configuration_tracked table for the current configuration.
Configuration::removeFromDataStore public function Removes the configuration file from the dataStore folder.
Configuration::scanDataStore public static function Returns the list of components available in the DataStore.
Configuration::setContext public function Set the context where a function is executed.
Configuration::setData public function Set the data for this configuration.
Configuration::setDependencies public function Returns the list of dependencies of this configuration
Configuration::setHash public function Set the hash for this configuration.
Configuration::setIdentifier public function Set the component identifier of this configuration
Configuration::setKeysToExport public function Set an array of keys names to export. If the array is empty, all the keys of the configuration will be exported.
Configuration::setModules public function Set the name of the required_modules that provide this configuration.
Configuration::setOptionalConfigurations public function Returns the list of optional_configurations of this configuration
Configuration::startTracking public function Save a configuration object into the configuration_tracked table.
Configuration::stopTracking public function Removes the configuration record from the configuration_tracked table for the current configuration.
MenuLinkConfiguration::alterDependencies public static function Overrides Drupal\configuration\Config\Configuration::alterDependencies(). Overrides Configuration::alterDependencies
MenuLinkConfiguration::getAllIdentifiers public static function Overrides Drupal\configuration\Config\Configuration::getAllIdentifiers(). Overrides Configuration::getAllIdentifiers
MenuLinkConfiguration::getComponent public function Overrides Drupal\configuration\Config\Configuration::getComponent(). Overrides Configuration::getComponent
MenuLinkConfiguration::getComponentHumanName public static function Overrides Drupal\configuration\Config\Configuration::getComponentHumanName(). Overrides Configuration::getComponentHumanName
MenuLinkConfiguration::getMenuLinkByIdenfifier public static function Helper function to retrive a menu link based on its identifier.
MenuLinkConfiguration::isActive public static function Overrides Drupal\configuration\Config\Configuration::isActive(). Overrides Configuration::isActive
MenuLinkConfiguration::prepareBuild protected function Overrides Drupal\configuration\Config\Configuration::prepareBuild(). Overrides Configuration::prepareBuild
MenuLinkConfiguration::saveToActiveStore public function Overrides Drupal\configuration\Config\Configuration::saveToActiveStore(). Overrides Configuration::saveToActiveStore
MenuLinkConfiguration::supportedComponents public static function Overrides Drupal\configuration\Config\Configuration::supportedComponents(). Overrides Configuration::supportedComponents
MenuLinkConfiguration::__construct public function Overrides Drupal\configuration\Config\Configuration::__construct(). Overrides Configuration::__construct