You are here

function menu_link_field_widget_form in Menu Link (Field) 7

Implements hook_field_widget_form().

File

./menu_link.field.inc, line 499
Defines a menu link field type.

Code

function menu_link_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $module_path = drupal_get_path('module', 'menu_link');
  $element += array(
    '#input' => TRUE,
    '#type' => $field['cardinality'] == 1 ? 'fieldset' : 'container',
    '#element_validate' => array(
      'menu_link_field_widget_validate',
    ),
    '#attached' => array(
      'js' => array(
        $module_path . '/menu_link.field.js',
      ),
      'css' => array(
        $module_path . '/menu_link.field.css',
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'menu-link-item',
        'menu-link-auto-title',
        'menu-link-auto-fieldset-summary',
      ),
    ),
  );

  // Populate the element with the link data.
  foreach (array(
    'mlid',
    'link_path',
    'options',
    'hidden',
    'expanded',
  ) as $key) {
    if (isset($items[$delta][$key])) {
      $element[$key] = array(
        '#type' => 'value',
        '#value' => $items[$delta][$key],
      );
    }
  }
  $menus = menu_get_menus();
  $available_menus = array_combine($instance['settings']['menu_options'], $instance['settings']['menu_options']);
  $menu_item = array(
    'mlid' => !empty($items[0]['mlid']) ? $items[0]['mlid'] : 0,
    'has_children' => FALSE,
  );
  $options = _menu_get_options($menus, $available_menus, $menu_item);
  $element['parent'] = array(
    '#type' => 'select',
    '#title' => t('Parent menu item'),
    '#options' => $options,
    '#empty_value' => '_none',
    '#attributes' => array(
      'class' => array(
        'menu-link-item-parent',
      ),
      'title' => t('Choose the menu item to be the parent for this link.'),
    ),
    '#required' => !empty($element['#required']),
  );
  if (isset($items[$delta]['menu_name'], $items[$delta]['plid'])) {
    $element['parent']['#default_value'] = $items[$delta]['menu_name'] . ':' . $items[$delta]['plid'];
  }
  $element['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#delta' => 50,
    '#default_value' => isset($items[$delta]['weight']) ? $items[$delta]['weight'] : 0,
    '#attributes' => array(
      'class' => array(
        'menu-link-item-weight',
      ),
      'title' => t('Menu links with smaller weights are displayed before links with larger weights.'),
    ),
  );
  $element['link_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => isset($items[$delta]['link_title']) ? $items[$delta]['link_title'] : '',
    '#size' => 50,
    '#maxlength' => 255,
    '#attributes' => array(
      'class' => array(
        'menu-link-item-title',
      ),
    ),
    '#description' => t('The text to be used for this link in the menu.'),
    '#required' => !empty($element['#required']),
  );
  if (!empty($field['settings']['link_path_field'])) {
    $element['link_path'] = array(
      '#type' => 'textfield',
      '#title' => t('Path'),
      '#default_value' => isset($items[$delta]['link_path']) ? $items[$delta]['link_path'] : '',
      '#size' => 50,
      '#maxlength' => 255,
      '#attributes' => array(
        'class' => array(
          'menu-link-item-path',
        ),
      ),
      '#description' => t('The path for this menu link.'),
    );
  }
  if (!empty($instance['widget']['settings']['fragment_field'])) {
    $element['fragment'] = array(
      '#type' => 'textfield',
      '#title' => t('URL Fragment'),
      '#field_prefix' => '#',
      '#default_value' => isset($items[$delta]['options']['attributes']['fragment']) ? $items[$delta]['options']['attributes']['fragment'] : '',
      '#size' => 10,
      '#maxlength' => 255,
      '#attributes' => array(
        'class' => array(
          'menu-link-item-fragment',
        ),
      ),
    );
  }
  if (!empty($instance['widget']['settings']['expanded_field'])) {
    $element['expanded'] = array(
      '#type' => 'checkbox',
      '#title' => t('Expanded'),
      '#title_display' => 'before',
      '#default_value' => isset($items[$delta]['expanded']) ? $items[$delta]['expanded'] : 0,
      '#attributes' => array(
        'class' => array(
          'menu-link-item-expanded',
        ),
        'title' => t('If selected and this menu link has children, the menu will always appear expanded.'),
      ),
      '#weight' => 5,
    );
  }
  if (!empty($instance['widget']['settings']['description_field'])) {
    $element['description'] = array(
      '#type' => 'textarea',
      '#title' => t('Description'),
      '#default_value' => isset($items[$delta]['options']['attributes']['title']) ? $items[$delta]['options']['attributes']['title'] : '',
      '#rows' => 1,
      '#description' => t('Shown when hovering over the menu link.'),
      '#attributes' => array(
        'class' => array(
          'menu-link-item-description',
        ),
      ),
      '#weight' => 10,
    );
  }
  return $element;
}