You are here

function node_reference_field_settings_form in References 7.2

Implements hook_field_settings_form().

File

node_reference/node_reference.module, line 55
Defines a field type for referencing one node from another.

Code

function node_reference_field_settings_form($field, $instance, $has_data) {
  $settings = $field['settings'];
  $form = array();
  $form['referenceable_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Content types that can be referenced'),
    '#multiple' => TRUE,
    '#default_value' => $settings['referenceable_types'],
    '#options' => array_map('check_plain', node_type_get_names()),
  );
  if (module_exists('views')) {
    $view_settings = $settings['view'];
    $description = '<p>' . t('The list of nodes that can be referenced can provided by a view (Views module) using the "References" display type.') . '</p>';

    // Special note for legacy fields migrated from D6.
    if (!empty($view_settings['view_name']) && $view_settings['display_name'] == 'default') {
      $description .= '<p><strong><span class="admin-missing">' . t("Important D6 migration note:") . '</span></strong>';
      $description .= '<br/>' . t("The field is currently configured to use the 'Master' display of the view %view_name.", array(
        '%view_name' => $view_settings['view_name'],
      ));
      $description .= '<br/>' . t("It is highly recommended that you: <br/>- edit this view and create a new display using the 'References' display type, <br/>- update the field settings to explicitly select the correct view and display.");
      $description .= '<br/>' . t("The field will work correctly until then, but submitting this form might inadvertently change the field settings.") . '</p>';
    }
    $form['view'] = array(
      '#type' => 'fieldset',
      '#title' => t('Views - Nodes that can be referenced'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($view_settings['view_name']),
      '#description' => $description,
    );
    $views_options = references_get_views_options('node');
    if ($views_options) {

      // The value of the 'view_and_display' select below will need to be split
      // into 'view_name' and 'view_display' in the final submitted values, so
      // we massage the data at validate time on the wrapping element (not
      // ideal).
      $form['view']['#element_validate'] = array(
        '_node_reference_view_settings_validate',
      );
      $views_options = array(
        '' => '<' . t('none') . '>',
      ) + $views_options;
      $default = empty($view_settings['view_name']) ? '' : $view_settings['view_name'] . ':' . $view_settings['display_name'];
      $form['view']['view_and_display'] = array(
        '#type' => 'select',
        '#title' => t('View used to select the nodes'),
        '#options' => $views_options,
        '#default_value' => $default,
        '#description' => '<p>' . t('Choose the view and display that select the nodes that can be referenced.<br />Only views with a display of type "References" are eligible.') . '</p>' . t('Note:<ul><li>This will discard the "Content types" settings above. Use the view\'s "filters" section instead.</li><li>Use the view\'s "fields" section to display additional informations about candidate nodes on node creation/edition form.</li><li>Use the view\'s "sort criteria" section to determine the order in which candidate nodes will be displayed.</li></ul>'),
      );
      $default = implode(', ', $view_settings['args']);
      $form['view']['args'] = array(
        '#type' => 'textfield',
        '#title' => t('View arguments'),
        '#default_value' => $default,
        '#required' => FALSE,
        '#description' => t('Provide a comma separated list of arguments to pass to the view.'),
      );
    }
    else {
      $form['view']['no_view_help'] = array(
        '#markup' => '<p>' . t('No eligible view was found.') . '</p>',
      );
    }
  }
  return $form;
}