You are here

function menu_link_menu_link_update in Menu Link (Field) 7

Implements hook_menu_link_update().

Synchronize menu_link fields with the updated menu link.

File

./menu_link.module, line 207

Code

function menu_link_menu_link_update($item) {
  $prior_item = menu_link_menu_link_alter();
  if ($item['module'] == 'menu_link' && empty($item['menu_link_field_save'])) {
    static $entity_paths;
    if (empty($entity_paths)) {
      $entity_get_info = entity_get_info();
      foreach ($entity_get_info as $entity_type => $entity_info) {
        if (isset($entity_info['path'])) {
          $entity_path = str_replace('%' . $entity_type, '%', $entity_info['path']);
          $entity_paths[$entity_path] = $entity_type;
        }
      }
    }
    if (isset($entity_paths[$item['router_path']])) {

      // Get entity type
      $entity_type = $entity_paths[$item['router_path']];

      // Get path to entity without wildcard
      $router_path = str_replace('%', '', $item['router_path']);

      // Get Entity ID
      $entity_id = str_replace($router_path, '', $item['link_path']);

      // Load entity
      $entity = array_shift(entity_load($entity_type, array(
        $entity_id,
      )));

      // Get bundle
      list(, , $bundle) = entity_extract_ids($entity_type, $entity);
      $save_entity = FALSE;
      foreach (field_info_instances($entity_type, $bundle) as $instance) {
        $field_name = $instance['field_name'];
        $field = field_info_field($field_name);
        if ($field['module'] == 'menu_link') {

          // Check if field exist on this entity
          if (isset($entity->{$field_name}) && !empty($entity->{$field_name})) {

            // Check if content is the same as current path for each language
            foreach ($entity->{$field_name} as $lang => $items) {

              // for each contents of fields
              foreach ($items as $i => $field_item) {

                // Check if it's current item
                if ($field_item['mlid'] == $item['mlid']) {

                  // So give it updated options
                  foreach ($entity->{$field_name}[$lang][$i] as $option_name => $option_value) {
                    $entity->{$field_name}[$lang][$i][$option_name] = $item[$option_name];
                  }
                  $save_entity = TRUE;
                  break;

                  // We found item in field content so break to next field
                }
              }
            }
          }
        }
      }

      // Entity has been edited so save it
      if ($save_entity) {
        entity_save($entity_type, $entity);
      }
    }
  }
}