You are here

function userreference_field_settings in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 5 userreference.module \userreference_field_settings()
  2. 6 modules/userreference/userreference.module \userreference_field_settings()
  3. 6.2 modules/userreference/userreference.module \userreference_field_settings()

Implementation of hook_field_settings().

File

modules/userreference/userreference.module, line 75
Defines a field type for referencing a user from a node.

Code

function userreference_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['referenceable_roles'] = array(
        '#type' => 'checkboxes',
        '#title' => t('User roles that can be referenced'),
        '#default_value' => isset($field['referenceable_roles']) && is_array($field['referenceable_roles']) ? array_filter($field['referenceable_roles']) : array(),
        '#options' => user_roles(1),
      );
      $form['referenceable_status'] = array(
        '#type' => 'radios',
        '#title' => t('User status that can be referenced'),
        '#default_value' => isset($field['referenceable_status']) ? $field['referenceable_status'] : '',
        '#options' => array(
          '' => t('All users'),
          1 => t('Active users'),
          0 => t('Blocked users'),
        ),
      );
      if (module_exists('views')) {
        $views = array(
          '--' => '--',
        );
        $all_views = views_get_all_views();
        foreach ($all_views as $view) {

          // Only 'users' views that have fields will work for our purpose.
          if ($view->base_table == 'users' && !empty($view->display['default']->display_options['fields'])) {
            if ($view->type == 'Default') {
              $views[t('Default Views')][$view->name] = $view->name;
            }
            else {
              $views[t('Existing Views')][$view->name] = $view->name;
            }
          }
        }
        $form['advanced'] = array(
          '#type' => 'fieldset',
          '#title' => t('Advanced - Users that can be referenced (View)'),
          '#collapsible' => TRUE,
          '#collapsed' => !isset($field['advanced_view']) || $field['advanced_view'] == '--',
        );
        if (count($views) > 1) {
          $form['advanced']['advanced_view'] = array(
            '#type' => 'select',
            '#title' => t('View used to select the users'),
            '#options' => $views,
            '#default_value' => isset($field['advanced_view']) ? $field['advanced_view'] : '--',
            '#description' => t('<p>Choose the "Views module" view that selects the users that can be referenced.<br />Note:</p>') . t('<ul><li>Only views that have fields will work for this purpose.</li><li>This will discard the "Referenceable Roles" and "Referenceable Status" settings above. Use the view\'s "filters" section instead.</li><li>Use the view\'s "fields" section to display additional informations about candidate users on user creation/edition form.</li><li>Use the view\'s "sort criteria" section to determine the order in which candidate users will be displayed.</li></ul>'),
          );
          $form['advanced']['advanced_view_args'] = array(
            '#type' => 'textfield',
            '#title' => t('View arguments'),
            '#default_value' => isset($field['advanced_view_args']) ? $field['advanced_view_args'] : '',
            '#required' => FALSE,
            '#description' => t('Provide a comma separated list of arguments to pass to the view.'),
          );
        }
        else {
          $form['advanced']['no_view_help'] = array(
            '#value' => t('<p>The list of user that can be referenced can be based on a "Views module" view but no appropriate views were found. <br />Note:</p>') . t('<ul><li>Only views that have fields will work for this purpose.</li><li>This will discard the "Referenceable Roles" and "Referenceable Status" settings above. Use the view\'s "filters" section instead.</li><li>Use the view\'s "fields" section to display additional informations about candidate users on user creation/edition form.</li><li>Use the view\'s "sort criteria" section to determine the order in which candidate users will be displayed.</li></ul>'),
          );
        }
      }
      return $form;
    case 'save':
      $settings = array(
        'referenceable_roles',
        'referenceable_status',
      );
      if (module_exists('views')) {
        $settings[] = 'advanced_view';
        $settings[] = 'advanced_view_args';
      }
      return $settings;
    case 'database columns':
      $columns = array(
        'uid' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => FALSE,
          'index' => TRUE,
        ),
      );
      return $columns;
    case 'views data':
      $data = content_views_field_views_data($field);
      $db_info = content_database_info($field);
      $table_alias = content_views_tablename($field);

      // Filter : swap the handler to the 'in' operator.
      $data[$table_alias][$field['field_name'] . '_uid']['filter']['handler'] = 'content_handler_filter_many_to_one';

      // Argument: get the user name for summaries.
      // We need to join a new instance of the users table.
      $data["users_{$table_alias}"]['table']['join']['node'] = array(
        'table' => 'users',
        'field' => 'uid',
        'left_table' => $table_alias,
        'left_field' => $field['field_name'] . '_uid',
      );
      $data[$table_alias][$field['field_name'] . '_uid']['argument']['handler'] = 'content_handler_argument_reference';
      $data[$table_alias][$field['field_name'] . '_uid']['argument']['name table'] = "users_{$table_alias}";
      $data[$table_alias][$field['field_name'] . '_uid']['argument']['name field'] = 'name';

      // Relationship: Add a relationship for related user.
      $data[$table_alias][$field['field_name'] . '_uid']['relationship'] = array(
        'base' => 'users',
        'field' => $db_info['columns']['uid']['column'],
        'handler' => 'content_handler_relationship',
        'label' => t($field['widget']['label']),
        'content_field_name' => $field['field_name'],
      );
      return $data;
  }
}