You are here

function _menu_link_node_menu_update_field in Menu Link (Field) 7

Adds, updates or deletes the default menu link field from a content type based on its menu settings.

Parameters

$info object: The content type object.

2 calls to _menu_link_node_menu_update_field()
menu_link_node_menu_node_type_insert in menu_link_node_menu/menu_link_node_menu.module
Implements hook_node_type_insert().
menu_link_node_menu_node_type_update in menu_link_node_menu/menu_link_node_menu.module
Implements hook_node_type_update().

File

menu_link_node_menu/menu_link_node_menu.module, line 181
Use a menu link field for core.

Code

function _menu_link_node_menu_update_field($info) {
  $prior_instance = field_read_instance('node', MENU_LINK_DEFAULT_FIELD, $info->type, array(
    'include_inactive' => TRUE,
  ));
  $menu_options = variable_get('menu_options_' . $info->type, array(
    'main-menu',
  ));
  if (!empty($menu_options)) {
    $instance = _menu_link_node_menu_instance($info);
    $t_args = array(
      '%label' => $instance['label'],
      '%name' => $info->name,
    );
    if (empty($prior_instance)) {
      try {
        field_create_instance($instance);
        drupal_set_message(t('The %label field has been added to the %name content type.', $t_args));
      } catch (Exception $e) {
        drupal_set_message(t('There was a problem creating field %label: @message.', array(
          '%label' => $instance['label'],
          '@message' => $e
            ->getMessage(),
        )));
      }
    }
    else {

      // TODO recursive?
      // TODO only update field if actually changed.
      // TODO reactivate inactivate field for new content types using a machine name that has been used before?
      $instance = $prior_instance + $instance;
      try {
        field_update_instance($instance);

        //drupal_set_message(t('The %label field has updated.', $t_args));
      } catch (Exception $e) {
        drupal_set_message(t('There was a problem creating field %label: @message.', array(
          '%label' => $instance['label'],
          '@message' => $e
            ->getMessage(),
        )));
      }
    }
  }
  elseif (!empty($prior_instance)) {
    $t_args = array(
      '%label' => $prior_instance['label'],
      '%name' => $info->name,
    );
    field_delete_instance($prior_instance);
    drupal_set_message(t('The %label field has been removed from the %name content type.', $t_args));
  }
}