You are here

menu_link_node_menu.install in Menu Link (Field) 7

Install, update and uninstall functions for the Menu link module.

File

menu_link_node_menu/menu_link_node_menu.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Menu link module.
 */

/**
 * Ensures availability of the MENU_LINK_DEFAULT_FIELD constant.
 */
require_once dirname(__FILE__) . '/../menu_link.module';

/**
 * Implements hook_requirements().
 */
function menu_link_node_menu_requirements($phase) {
  $requirements = array();
  if ($phase == 'install') {
    $prior_field = field_read_field(MENU_LINK_DEFAULT_FIELD, array(
      'include_inactive' => TRUE,
    ));
    if (!empty($prior_field) && !($prior_field['type'] == 'menu_link')) {
      $t = get_t();
      $requirements['menu_link_default_field'] = array(
        'title' => $t('Field name already in use'),
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('The field name ":menu_link_field" is already in use. This field name is required for the Menu link module. Please rename or remove the prior field before installing the Menu link module.', array(
          ':menu_link_field' => MENU_LINK_DEFAULT_FIELD,
        )),
      );
    }
  }
  if ($phase == 'runtime') {
    $requirements['menu_link_node_menu'] = array(
      'title' => t('Node Menu links'),
      'value' => 'Synchronized',
      'description' => l(t('Populate the menu link field of nodes.'), 'admin/reports/status/rebuild/node-menu-link'),
    );
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function menu_link_node_menu_install() {

  // Make sure the Menu link field type is available.
  field_info_cache_clear();
  $prior_field = field_read_field(MENU_LINK_DEFAULT_FIELD, array(
    'include_inactive' => TRUE,
  ));
  if (empty($prior_field)) {

    // Create the default menu link field.
    field_create_field(array(
      'field_name' => MENU_LINK_DEFAULT_FIELD,
      'type' => 'menu_link',
      'storage' => array(
        'type' => 'field_sql_storage',
      ),
    ));
  }
  elseif (!$prior_field['active']) {

    // Activate the default menu link field.
    $prior_field['active'] = 1;
    field_update_field($prior_field);
  }
}

/**
 * Implements hook_uninstall().
 *
 * @see menu_link_node_menu_disable()
 *   Passed ownership of menu links in the default menu link field to the Menu
 *   module.
 * @see menu_link_field_delete()
 *   Deletes menu links when a menu link field is deleted, but only if they are
 *   owned by the Menu link module.
 */
function menu_link_node_menu_uninstall() {

  // Remove instances of the default menu link field from all content types.
  // Menu links won't be deleted as their ownership is passed to the Menu module
  // in menu_link_node_menu_disable()
  foreach (field_read_instances(array(
    'entity_type' => 'node',
    'field_name' => MENU_LINK_DEFAULT_FIELD,
  )) as $instance) {
    field_delete_instance($instance);
  }
}

/**
 * Implements hook_enable().
 *
 * @see menu_link_node_type_update()
 *   Creates instances of the MENU_LINK_DEFAULT_FIELD for content types that
 *   allow nodes to be placed in the menu.
 */
function menu_link_node_menu_enable() {
  foreach (node_type_get_types() as $info) {

    // Invokes menu_link_node_type_update().
    node_type_save($info);
  }
}

/**
 * Implements hook_disable().
 */
function menu_link_node_menu_disable() {
  $instances = field_read_instances(array(
    'entity_type' => 'node',
    'field_name' => MENU_LINK_DEFAULT_FIELD,
  ));
  if (!empty($instances)) {
    $bundles = array();
    foreach ($instances as $instance) {
      $bundles[] = $instance['bundle'];
    }

    // Pass on ownership of menu links previously owned by the Menu link module
    // to the Menu module. We don't need to use menu_link_save() as an ownership
    // change doesn't have to trigger hooks.
    db_update('menu_links')
      ->fields(array(
      'module' => 'menu',
    ))
      ->condition('module', 'menu_link')
      ->exists(db_select('field_data_' . MENU_LINK_DEFAULT_FIELD, 'f')
      ->fields('f')
      ->condition('f.entity_type', 'node')
      ->condition('f.bundle', $bundles)
      ->where('f.' . MENU_LINK_DEFAULT_FIELD . '_mlid = mlid'))
      ->execute();

    // De-activate instances of the default menu link field on content types.
    foreach ($instances as $instance) {
      $instance['active'] = 0;
      field_update_instance($instance);
    }
  }
}