You are here

function _nodesymlinks_form_field in NodeSymlinks 7

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

Helper function to generate custom nodesymlinks form item.

1 call to _nodesymlinks_form_field()
_nodesymlinks_node_form_alter in ./nodesymlinks.inc
Implements hook_form_alter().

File

./nodesymlinks.inc, line 196
Main NodeSymlinks callbacks

Code

function _nodesymlinks_form_field(array $item, stdClass $node) {

  // Load default properties.
  $item += array(
    'link_title' => isset($item['link_title']) ? $item['link_title'] : '',
    'mlid' => 0,
    'plid' => 0,
    'menu_name' => 'navigation',
    'weight' => 0,
    'options' => array(),
    'module' => 'nodesymlinks',
    'expanded' => 0,
    'hidden' => 0,
    'has_children' => 0,
    'customized' => 0,
    'p1' => 0,
    'depth' => 0,
  );
  $item_form = array();
  $item_form['#tree'] = TRUE;

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

  // Generate list of possible parents (not including this item or descendants).
  $menu_parent_options = menu_parent_options(menu_get_menus(), $item, $node->type);

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

      // Find if there is an automatic alias pattern for this node type.
      $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.
            $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_form['alias']['path'] = array(
      '#type' => 'textfield',
      '#default_value' => $path,
      '#maxlength' => 128,
      '#size' => 10,
      '#access' => user_access('create url aliases'),
    );
    if ($path) {
      $item_form['alias']['pid'] = array(
        '#type' => 'value',
        '#value' => db_query("SELECT pid FROM {url_alias} WHERE alias = :alias AND language = :language", array(
          ':alias' => $path,
          ':language' => $language,
        ))
          ->fetchField(),
      );
    }
  }
  $item_form['fragment'] = array(
    '#type' => 'textfield',
    '#default_value' => isset($item['fragment']) ? $item['fragment'] : '',
    '#maxlength' => 128,
    '#size' => 10,
    '#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;
}