You are here

function menu_import_parse_line in Menu Export/Import 7

Parse a line of text containing the menu structure. Only * and - are allowed as indentation characters. Menu item definition may or may not contain details in JSON format.

Parameters

$line: One line from input file.

$prev_level: Previous level to build ierarchy.

$weights: Array of menu items' weights.

$parents: Array of menu items' parents.

$options: Array of importing options.

Return value

Array representing a menu item.

2 calls to menu_import_parse_line()
menu_import_parse_menu_from_file in includes/import.inc
File parser function. Reads through the text file and constructs the menu.
menu_import_parse_menu_from_string in includes/import.inc
Text parser function. Reads through the text and constructs the menu.

File

includes/import.inc, line 151
Import functions for menu_import module.

Code

function menu_import_parse_line($line, $prev_level, array $weights, array $parents, array $options) {
  $menuitem = array(
    'error' => FALSE,
    'link_title' => NULL,
    'children' => array(),
    'parent' => 0,
    'nid' => FALSE,
    'path' => FALSE,
    'weight' => 0,
    'external' => FALSE,
    'level' => 0,
    'options' => array(),
  );

  // Set default language
  if (module_exists('i18n_menu')) {
    $menuitem['language'] = $options['language'];
  }
  $langs = array_keys(language_list());
  $path = $description = $expanded = $hidden = '';
  $language = NULL;
  $weight = NULL;

  // JSON is used.
  // @todo: make the input JSON not so strict.
  if (($json_start = strpos($line, '{"')) != 0) {
    $json = substr($line, $json_start);
    $details = json_decode($json, TRUE);

    // Parse structure and title.
    $base_info = substr($line, 0, $json_start);
    $level_title = _menu_import_parse_level_title($base_info);

    // Extract details.
    if (!is_null($details)) {
      $path = empty($details['url']) ? '' : trim($details['url']);
      $node_uuid = !empty($details['node_uuid']) ? trim($details['node_uuid']) : NULL;
      $description = empty($details['description']) ? '' : trim($details['description']);
      $expanded = !empty($details['expanded']);
      $hidden = !empty($details['hidden']);
      $language = !empty($details['lang']) && in_array($details['lang'], $langs) ? $details['lang'] : NULL;
      $weight = isset($details['weight']) ? intval($details['weight']) : NULL;
      $link_options = empty($details['options']) ? array() : $details['options'];
    }
    else {
      return _menu_import_mark_error_item($menuitem, t('malformed item details'), $line);
    }
  }
  else {
    $level_title = _menu_import_parse_level_title($line);
  }
  if (!$level_title) {
    return _menu_import_mark_error_item($menuitem, t('missing title or wrong indentation'), $line);
  }
  else {
    list($level, $title) = $level_title;
  }

  // Skip empty items
  if (!strlen($title)) {
    return _menu_import_mark_error_item($menuitem, t('missing item title'), $line);
  }

  // Make sure this item is only 1 level below the last item.
  if ($level > $prev_level + 1) {
    return _menu_import_mark_error_item($menuitem, t('wrong indentation'), $line);
  }
  if (isset($weights[$level]) && is_null($weight)) {
    if ($level > $prev_level) {
      $weight = 0;
    }
    else {
      $weight = $weights[$level] + 1;
    }
  }
  elseif (is_null($weight)) {
    $weight = 0;
  }
  $menuitem['weight'] = $weight;
  $menuitem['parent'] = !$level ? 0 : $parents[$level - 1];
  $menuitem['link_title'] = $title;
  $menuitem['level'] = $level;
  $menuitem['path'] = $path;
  if (!empty($link_options)) {
    $menuitem['options'] = $link_options;
  }
  if (url_is_external($path)) {
    $menuitem['external'] = TRUE;
    $menuitem['link_path'] = $path;
  }
  elseif (!empty($node_uuid) && ($uuid_lookup_result = entity_get_id_by_uuid('node', array(
    $node_uuid,
  )))) {
    $menuitem['link_path'] = 'node/' . $uuid_lookup_result[$node_uuid];
    $menuitem['nid'] = $uuid_lookup_result[$node_uuid];
  }
  else {
    $result = _menu_import_lookup_path($path, $title, $language, $options);
    $menuitem['link_path'] = $result['link_path'];
    $menuitem['nid'] = $result['nid'];
    $menuitem['node_view'] = $result['node_view'];
    if ($result['options']) {
      $menuitem['options'] = array_merge($menuitem['options'], $result['options']);
    }
  }
  if ($description) {
    $menuitem['description'] = $description;
  }
  if ($hidden) {
    $menuitem['hidden'] = 1;
  }
  if ($expanded) {
    $menuitem['expanded'] = 1;
  }
  if ($language) {
    $menuitem['language'] = $language;

    // Important when setting the language, otherwise it'll be ignored.
    $menuitem['customized'] = 1;
  }
  return $menuitem;
}