You are here

function viewfield_widget in Viewfield 5

Same name and namespace in other branches
  1. 6.2 viewfield.module \viewfield_widget()
  2. 6 viewfield.module \viewfield_widget()

Implementation of hook_widget().

2 calls to viewfield_widget()
viewfield_field in ./viewfield.module
Implementation of hook_field().
viewfield_field_settings in ./viewfield.module
Implementation of hook_field_settings().

File

./viewfield.module, line 257
Defines a field type for referencing a view from a node

Code

function viewfield_widget($op, &$node, $field, &$node_field) {
  switch ($op) {
    case 'prepare form values':
      $node_field_transposed = content_transpose_array_rows_cols($node_field);
      $node_field['default vnames'] = $node_field_transposed['vname'];

      // Get rid of weird ghosts of null values.
      if (isset($node_field['default vnames']) && array_key_exists(0, $node_field['default vnames']) && !isset($node_field['default vnames'][0])) {
        unset($node_field['default vnames'][0]);
      }
      break;
    case 'form':

      // If no allowed views are selected, allow all views.
      if (!isset($field['allowed_views']) || count(array_flip($field['allowed_views'])) == 1) {
        $field['allowed_views'] = _viewfield_potential_references(array());
      }
      $form = array();

      // Create the fieldset for the view field, showing it depending on whether or
      // not force default is selected.
      $form[$field['field_name']] = array(
        '#type' => 'fieldset',
        '#title' => $field['widget']['label'],
        '#tree' => TRUE,
        '#access' => !$field['widget']['force_default'] || !isset($node->uid),
      );

      // This form is used for both the default value field in the admin as well as
      // the node edit form, so we have to make sure we show the default value field
      // always.
      if ($field['widget']['force_default'] && isset($node->uid)) {
        $form[$field['field_name']]['vnames'] = array(
          '#type' => 'value',
          '#value' => $field['widget']['default_value'][0]['vname'],
        );
        $form[$field['field_name']]['vargs'] = array(
          '#type' => 'value',
          '#value' => $field['widget']['default_value'][0]['vargs'],
        );
      }
      else {

        // Display the form to let the user pick a view.
        $options = _viewfield_potential_references($field);
        $allowed_views = array_flip($field['allowed_views']);
        unset($allowed_views[0]);
        foreach ($options as $key => $value) {
          if (!(0 === $key || in_array($key, $allowed_views))) {
            unset($options[$key]);
          }
        }

        // Provide our own overriding of defaults.
        if ($field['super_default']) {
          $form[$field['field_name']]['override_default'] = array(
            '#type' => 'checkbox',
            '#title' => t('Override default'),
            '#default_value' => !($node_field[0]['default'] || arg(1) == 'add'),
          );
        }
        if (count($options) > 1) {
          $form[$field['field_name']]['vnames'] = array(
            '#type' => 'select',
            '#title' => $field['widget']['label'],
            '#default_value' => $node_field['default vnames'],
            '#multiple' => $field['multiple'],
            '#options' => $options,
            '#required' => $field['required'],
            '#description' => $field['widget']['description'],
          );
          $form[$field['field_name']]['vargs'] = array(
            '#type' => 'textfield',
            '#title' => t('Arguments'),
            '#default_value' => $node_field[0]['vargs'],
            // All views share args (for now).
            '#required' => false,
            '#description' => t('Provide a comma separated list of arguments to pass to the view. These arguments will be passed to EACH selected view. If an argument contains commas or double quotes, enclose it in double quotes. Replace double quotes that are part of the argument with pairs of double quotes.'),
          );
        }
        else {

          // There's only the one view, so only show the arguments.
          list($key, $label) = each($options);
          $form[$field['field_name']]['vnames'] = array(
            '#type' => 'value',
            '#value' => $key,
            '#title' => $value,
          );
          $form[$field['field_name']]['vargs'] = array(
            '#type' => 'textfield',
            '#title' => $field['widget']['label'] . " ({$label}) " . t('arguments'),
            '#default_value' => $node_field[0]['vargs'],
            // All views share args (for now).
            '#required' => false,
            '#description' => t('Provide a comma separated list of arguments to pass to the view. These arguments will be passed to EACH selected view. If an argument contains commas or double quotes, enclose it in double quotes. Replace double quotes that are part of the argument with pairs of double quotes.'),
          );
        }

        // TODO: Token support right now is a bit hacked on, needs better integration,
        // eventually a checkbox to enable/disable use of token-module here.
        if (module_exists('token')) {
          $form[$field['field_name']]['vargs']['#description'] .= ' ' . t('Use the syntax [token] if you want to insert a replacement pattern.');
          $form[$field['field_name']]['token_help'] = array(
            '#title' => t('Replacement patterns'),
            '#type' => 'fieldset',
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
          );
          $form[$field['field_name']]['token_help']['help'] = array(
            '#value' => theme('token_help', 'node'),
          );
        }
        else {
          $form[$field['field_name']]['vargs']['#description'] .= '<br/>' . t('You may use %nid for the node id of the current node. %author for the node author and %viewer for user viewing the node.');
        }
      }
      return $form;
    case 'process form values':
      if (!$field['super_default'] || $node_field['override_default']) {
        if ($field['multiple']) {
          $items = $node_field['vnames'];
          if (is_array($items)) {
            foreach ($items as $item) {
              $node_field[] = array(
                'vname' => $item,
                'vargs' => $node_field['vargs'],
              );
            }
          }
        }
        else {
          $node_field[0]['vname'] = $node_field['vnames'];
          $node_field[0]['vargs'] = $node_field['vargs'];
        }

        // Remove the widget's data representation so it isn't saved.
        unset($node_field['vnames']);
        unset($node_field['vargs']);
        unset($node_field['override_default']);
        unset($node_field['default']);
      }
      else {
        if ($field['multiple']) {
          $node_field = array();
        }
        else {
          $node_field = array(
            0 => array(
              'vname' => NULL,
              'vargs' => NULL,
            ),
          );
        }
      }
      break;
  }
}