You are here

public static function PartyPrimaryFields::sourceForm in Party 7

Set up a source form under the given element.

This is typically called on a fieldset.

Parameters

array $element: The container or fieldset element or form we are adding the source table to. In addition to it's normal keys, this element should have:

  • #target: The name of the target property.
  • #source_types: Optionally provide a filter for source types. If not provided it will be worked out from the given target property.
  • #parents: As this function does not deal with any submission of the user's input, #parents should be set to indicate where the submitted data should appear in $form_state['values']. If not set, array() will be used resulting in $form_state['values'] having 'sources' as a root key.

array $form_state: The form state.

array $form: The whole form.

2 calls to PartyPrimaryFields::sourceForm()
party_form_field_ui_field_edit_form_alter in ./party.module
party_primary_fields_edit_field in ./party.admin.inc
Form constructor for setting primary field sources.

File

includes/party.primary_fields.inc, line 437
Primary field related functions and callbacks.

Class

PartyPrimaryFields
Helper class for primary fields.

Code

public static function sourceForm(&$element, &$form_state, &$form) {

  // Set up our include and submission handler.
  form_load_include($form_state, 'inc', 'party', 'party.admin');
  $form['#submit'][] = 'party_primary_fields_clear_caches';

  // Make sure the element has the required properties.
  $element['#tree'] = TRUE;
  if (!isset($element['#parents'])) {
    $element['#parents'] = array();
  }

  // Get hold of info about the target.
  if (!array_key_exists('#source_types', $element)) {
    $target_info = self::getPropertyInfo('party', 'party', $element['#target']);
    $element['#source_types'] = isset($target_info['type']) ? array(
      $target_info['type'],
    ) : NULL;
  }
  elseif (is_string($element['#source_types'])) {
    $element['#source_types'] = (array) $element['#source_types'];
  }

  // Get hold of the current state of the sources.
  if (isset($form_state['values'])) {
    $sources = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  }
  if (!isset($sources) || !is_array($sources)) {
    $sources = $element['#default_value'];
  }

  // Make sure the sources are sorted by weight.
  uasort($sources, 'drupal_sort_weight');

  // Construct our draggable table.
  $element['table'] = array(
    '#theme' => 'table',
    '#header' => array(
      'property_desc' => t('Property'),
      'data_set_desc' => t('Data set'),
      'callback' => t('Callback'),
      'weight' => t('Weight'),
      'remove' => t('Remove'),
    ),
    '#empty' => t('No sources are selects.'),
    '#attributes' => array(
      'class' => array(
        'field-ui-overview',
      ),
      'id' => 'field-sources',
    ),
    '#parents' => $element['#parents'],
    '#pre_render' => array(
      'party_primary_fields_source_table_pre_render',
    ),
  );
  drupal_add_tabledrag('field-sources', 'order', 'sibling', 'source-weight');

  // Build our rows which will be put into the table after building.
  foreach ($sources as $key => $source) {

    // Get hold of our source info.
    $data_set_info = self::getDataSetInfo($source['data_set']);
    $source_info = self::getSource($source);
    $property_key = self::getSourceKey($source);
    $property_key = substr($property_key, strpos($property_key, ':') + 1);
    $row = array(
      '#element_validate' => array(
        'party_primary_fields_source_table_source_cleanup',
      ),
      'data_set' => array(
        '#type' => 'value',
        '#value' => $source['data_set'],
      ),
      'property' => array(
        '#type' => 'value',
        '#value' => $source['property'],
      ),
      'value' => array(
        '#type' => 'value',
        '#value' => $source['value'],
      ),
      'property_desc' => array(
        '#markup' => format_string('@label <small>[@name]</small>', array(
          '@label' => $source_info['label'],
          '@name' => $property_key,
        )),
      ),
      'data_set_desc' => array(
        '#markup' => format_string('@label <small>[@name]</small>', array(
          '@label' => $data_set_info['label'],
          '@name' => $data_set_info['set_name'],
        )),
      ),
      'weight' => array(
        '#type' => 'weight',
        '#default_value' => $source['weight'],
        '#delta' => 50,
        '#title_display' => 'invisible',
        '#title' => t('Weight for @label', array(
          '@label' => $source_info['label'],
        )),
        '#attributes' => array(
          'class' => array(
            'source-weight',
          ),
        ),
      ),
      'callback' => array(),
      'remove' => array(
        '#type' => 'submit',
        '#value' => t('Remove'),
        '#name' => 'remove_source:' . $key,
        '#submit' => array(
          'party_primary_fields_source_table_submit',
        ),
      ),
    );

    // Find any valid callbacks.
    $callbacks = array();
    if (!empty($source_info['callbacks'])) {
      foreach ($source_info['callbacks'] as $key => $info) {
        if (!isset($element['#source_types']) || in_array($info['type'], $element['#source_types'])) {
          $callbacks[$key] = $info['label'];
        }
      }
    }

    // If we have valid callbacks, provide options.
    $row['callback'] = array(
      '#type' => 'select',
      '#title' => t('Callback'),
      '#title_display' => 'invisible',
      '#options' => $callbacks,
      '#default_value' => isset($source['callback']) ? $source['callback'] : NULL,
    );

    // If the source is valid on it's own, provide an empty option.
    if (empty($callbacks) || !isset($element['#source_types']) || in_array($source_info['type'], $element['#source_types'])) {
      $row['callback']['#empty_option'] = t('No callback');
    }
    $element['table'][$key] = $row;
  }

  // Find suitable sources.
  $suitable_sources = PartyPrimaryFields::getSourcesByType($element['#source_types']);

  // Build our options list.
  $options = array();
  foreach ($suitable_sources as $data_set) {
    $set_options = array();
    foreach ($data_set['sources'] as $source) {
      $key = $source['data_set'] . ':' . $source['property'];
      if (isset($source['value'])) {
        $key .= ':' . $source['value'];
      }
      $set_options[$key] = $source['option label'];
    }
    $options[$data_set['option label']] = $set_options;
  }
  $element['add_source'] = array(
    '#type' => 'container',
    '#tree' => FALSE,
    '#weight' => 99,
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
    'add_source_property' => array(
      '#type' => 'select',
      '#title' => t('Source property'),
      '#options' => $options,
      '#empty_option' => ' - ' . t('Select property') . ' - ',
    ),
    'add_source_submit' => array(
      '#type' => 'submit',
      '#value' => t('Add source'),
      '#limit_validation_errors' => array(
        array(
          'add_source_property',
        ),
        $element['#parents'],
      ),
      '#validate' => array(
        'party_primary_fields_source_table_validate',
      ),
      '#submit' => array(
        'party_primary_fields_source_table_submit',
      ),
    ),
  );
}