You are here

function _nodesymlinks_form_field in NodeSymlinks 6

Same name and namespace in other branches
  1. 7 nodesymlinks.inc \_nodesymlinks_form_field()

Helper function to generate custom nodesymlinks form item.

Parameters

item $item:

Return value

form_elements

1 call to _nodesymlinks_form_field()
_nodesymlinks_form_alter in ./nodesymlinks.inc
Implementation of hook_form_alter().

File

./nodesymlinks.inc, line 178

Code

function _nodesymlinks_form_field($item = NULL, $node) {

  // Load default properties.
  if (is_null($item)) {
    $item = nodesymlinks_menu_item_defaults();
  }
  else {
    $item += nodesymlinks_menu_item_defaults();
  }
  $item_form = array();
  $item_form['#tree'] = TRUE;

  // @TODO Maybe we can simplify this - whole array can be stored in value type
  static $item_keys = array(
    'mlid',
    'module',
    'hidden',
    'has_children',
    'customized',
    'options',
    'expanded',
  );
  foreach ($item_keys as $key) {
    $item_form[$key] = array(
      '#type' => 'value',
      '#value' => $item[$key],
    );
  }

  // Generate a list of possible parents (not including this item or descendants).
  $options = menu_parent_options(menu_get_menus(), $item);

  // Generate all tree $item['mlid'] == 0.
  $default = !empty($item['parents']) ? $item['parents'] : $item['menu_name'] . ':' . $item['plid'];
  if (!isset($options[$default])) {
    $default = array(
      'primary-links:0',
    );
  }
  $item_form['parents'] = array(
    '#type' => 'select',
    '#default_value' => $default,
    '#options' => $options,
    '#multiple' => FALSE,
    '#description' => '',
    '#attributes' => array(
      'class' => 'menu-title-select',
    ),
  );
  $item_form['link_title'] = array(
    '#type' => 'textfield',
    // Note that check_plain() is unnecessary here and it also translates
    // titles to HTML entities.
    '#default_value' => $item['link_title'],
    '#description' => '',
    '#required' => FALSE,
  );
  if (module_exists('path')) {
    $language = isset($node->language) ? $node->language : '';
    $item_form['alias'] = array(
      '#tree' => TRUE,
    );
    if (module_exists('pathauto')) {
      _nodesymlinks_include('pathauto');

      // Find if there is an automatic alias pattern for this node type.
      if (nodesymlinks_pathauto_version() == 1) {
        $pattern = _nodesymlinks_pathauto_pattern_load_by_entity($node->type, $language);
      }
      else {
        $pattern = pathauto_pattern_load_by_entity('nodesymlinks', $node->type, $language);
      }

      // If there is a pattern, show the automatic alias checkbox.
      if ($pattern) {
        if (!isset($item['alias']['pathauto'])) {
          if (!empty($node->nid) && !empty($item['mlid'])) {

            // If this is not a new node, compare it's current alias to the
            // alias that would be genereted by pathauto. If they are the same,
            // then keep the automatic alias enabled.
            _nodesymlinks_include('pathauto');
            $pathauto_alias = nodesymlinks_pathauto_create_alias($item, $node);
            $item['alias']['pathauto'] = isset($item['alias']['path']) && $item['alias']['path'] == $pathauto_alias;
          }
          else {

            // If this is a new node, enable the automatic alias.
            $item['alias']['pathauto'] = TRUE;
          }
        }

        // Automatic alias.
        $item_form['alias']['pathauto'] = array(
          '#type' => 'checkbox',
          '#default_value' => $item['alias']['pathauto'],
        );
      }
    }
    $path = !empty($item['alias']['path']) ? $item['alias']['path'] : $item['alias']['old_path'];
    $old_path = !empty($item['alias']['path']) ? $item['alias']['path'] : $item['alias']['old_path'];
    $item_form['alias']['path'] = array(
      '#type' => 'textfield',
      '#default_value' => $path,
      '#maxlength' => 128,
      '#access' => user_access('create url aliases'),
    );
    $item_form['alias']['old_path'] = array(
      '#type' => 'value',
      '#value' => $old_path,
    );
    if ($path) {
      $item_form['alias']['pid'] = array(
        '#type' => 'value',
        '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $path, $item['language'])),
      );
    }
  }
  $item_form['fragment'] = array(
    '#type' => 'textfield',
    '#default_value' => isset($item['fragment']) ? $item['fragment'] : '',
    '#maxlength' => 128,
    '#access' => user_access('create url aliases'),
  );
  $item_form['weight'] = array(
    '#type' => 'weight',
    '#delta' => 50,
    '#default_value' => $item['weight'],
    '#description' => '',
  );
  $item_form['delete'] = array(
    '#type' => 'checkbox',
    '#title' => '',
  );
  return $item_form;
}