You are here

function viewfield_field_widget_form in Viewfield 7.3

Same name and namespace in other branches
  1. 7.2 viewfield.module \viewfield_field_widget_form()

Implements hook_field_widget_form().

File

./viewfield.module, line 368
Defines a field type to display a view.

Code

function viewfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Administrators need to be able to re-configure the default value on the
  // field edit form if 'force_default' is enabled.
  if ($form_state['build_info']['form_id'] == 'field_ui_field_edit_form') {
    $instance['settings']['force_default'] = FALSE;
  }

  // Display the form to let the user pick a view.
  $options = _viewfield_potential_references($field, $instance);
  $element['vname'] = array(
    '#type' => 'select',
    '#title' => $element['#title'],
    '#options' => $options,
    '#required' => $element['#required'],
    '#empty_value' => 0,
    '#default_value' => isset($items[$delta]['vname']) ? $items[$delta]['vname'] : NULL,
    '#access' => !$instance['settings']['force_default'],
    '#description' => $element['#description'],
  );
  $element['vargs'] = array(
    '#type' => 'textfield',
    '#title' => t('Arguments'),
    '#default_value' => isset($items[$delta]['vargs']) ? $items[$delta]['vargs'] : NULL,
    '#access' => !$instance['settings']['force_default'],
    '#description' => t('A comma separated list of arguments to pass to the selected view. '),
    '#maxlength' => 255,
  );

  // Provide token help.
  if (module_exists('token')) {
    $element['vargs']['#description'] .= t('Any token from the placeholder token list may be used as an argument.');

    // Only show token help for first value or in field settings form.
    if ($element['#delta'] === 0 && !$instance['settings']['force_default']) {
      $entity_type = $element['#entity_type'] == 'taxonomy_term' ? 'term' : $element['#entity_type'];
      $element['token_help'] = array(
        '#type' => 'fieldset',
        '#title' => t('Placeholder tokens'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $element['token_help']['tokens'] = array(
        '#theme' => 'token_tree',
        '#token_types' => array(
          str_replace('_', '-', $entity_type),
        ),
        '#global_types' => TRUE,
        '#dialog' => TRUE,
      );
    }
  }
  else {
    $element['vargs']['#description'] .= t('Enable the %token module to see a complete list of tokens that may be used as arguments.', array(
      '%token' => t('Token'),
    ));
  }
  $element['vargs']['#description'] .= t(' (Wrap arguments containing commas in double quotes and replace double quotes in arguments with a pair of double quotes.)');

  // @todo Core: Fix bogus white-space: nowrap.
  // @see http://drupal.org/node/1230336
  $element['#attached']['css'] = array(
    drupal_get_path('module', 'viewfield') . '/viewfield.css' => array(
      'weight' => 1,
    ),
  );
  return $element;
}