You are here

function entityreference_autofill_populate_form_by_field in Entity reference autofill 7

Populate other form fields with respect to this module's field settings.

Parameters

int $referenced_target_id: id of referenced entity to fetch data from.

string $reference_field_name: name of entity reference field triggering this call.

string $langcode: current language.

array $autofill_field_settings: module settings for $reference_field_name in current context.

string $entity_type: the entity type of the referencing form.

string $bundle: the bundle of the referencing form

string $form: the $form array.

string $form_state: the $form_state array.

1 call to entityreference_autofill_populate_form_by_field()
entityreference_autofill_field_attach_form in ./entityreference_autofill.module
Implements hook_field_attach_form().

File

./entityreference_autofill.module, line 275
Entity reference autofill module.

Code

function entityreference_autofill_populate_form_by_field($referenced_target_id, $reference_field_name, $langcode, $autofill_field_settings, $entity_type, $bundle, &$form, &$form_state) {

  // Entity reference field.
  $reference_field = field_form_get_state($form['#parents'], $reference_field_name, LANGUAGE_NONE, $form_state);

  // Reference entity metadata.
  $referenced_entity_type = $reference_field['field']['settings']['target_type'];

  // No value, quit processing.
  if (!is_numeric($referenced_target_id)) {
    return;
  }

  // Load referenced entity.
  $referenced_entity = entity_load_single($referenced_entity_type, $referenced_target_id);

  // Populate fields with values from referenced node.
  $autofill_fields = $autofill_field_settings['fields'];
  $overwrite = $autofill_field_settings['overwrite'];

  // Empty current form_state for this bundle.
  if (!isset($form_state['entityreference_autofill'][$entity_type][$bundle])) {
    $form_state['entityreference_autofill'][$entity_type][$bundle] = array();
  }
  drupal_array_set_nested_value($form_state['entityreference_autofill'][$entity_type][$bundle], $form['#parents'], array());
  $autofill_map =& drupal_array_get_nested_value($form_state['entityreference_autofill'][$entity_type][$bundle], $form['#parents']);

  // Load relevant part of form's input array.
  $form_input =& drupal_array_get_nested_value($form_state['input'], $form['#parents']);

  // Save current form entity, in case field_default_form gets any ideas on
  // multiple value form elements.
  // @see https://www.drupal.org/node/2318109#comment-9068141
  $preserved_entity = isset($form['#entity']) ? $form['#entity'] : FALSE;
  foreach ($autofill_fields as $field_name) {
    if (isset($form[$field_name])) {

      // Language fail-safe for referenced entity.
      // Use field language or undefined if empty.
      // Makes referenced values sensitive to language
      // selection in entity form.
      // @see https://drupal.org/node/2205245
      $referenced_language = isset($form_state['values']['language']) ? $form_state['values']['language'] : $langcode;

      // Limit to allowed language(s) for this field, fallback to undefined.
      $referenced_language = field_language($referenced_entity_type, $referenced_entity, $field_name, $referenced_language);
      if (empty($referenced_language)) {
        $referenced_language = LANGUAGE_NONE;
      }

      // Get current field's language.
      $field_language = $form[$field_name]['#language'];

      // Consider overwrite setting.
      if (!$overwrite) {
        if (_entityreference_autofill_field_has_value($form_input[$field_name][$field_language], $field_name)) {
          continue;
        }
      }

      // Load new value.
      $items = field_get_items($referenced_entity_type, $referenced_entity, $field_name, $referenced_language);
      if (!empty($items)) {

        // Add callback info if AJAX.
        $autofill_map[$field_name] = _entityreference_autofill_get_wrapper($entity_type, $bundle, $field_name, $form['#parents']);

        // Field information for rendering form.
        $field = field_info_field($field_name);
        $instance = field_info_instance($entity_type, $field_name, $bundle);

        // Let other modules interact with $form_state prior to
        // generating this field's form.
        // @see entityreference_autofill.api.php
        $context = array(
          'form' => $form,
          'field' => $field,
          'instance' => $instance,
          'items' => $items,
          'langcode' => $referenced_language,
          'reference_field_name' => $reference_field_name,
        );
        drupal_alter('entityreference_autofill_fill_items', $form_state, $context);

        // Replace field state with referenced values.
        // This is needed for multi-value fields with different
        // cardinality in source and destination.
        $field_state = field_form_get_state($form['#parents'], $field_name, $field_language, $form_state);
        $field_state['items_count'] = count($items);
        field_form_set_state($form['#parents'], $field_name, $field_language, $form_state, $field_state);

        // Replace field with new defaults.
        // @todo $referenced_language or $field_language here?
        // Implications if they differ?
        $field_form = field_default_form($referenced_entity_type, $referenced_entity, $field, $instance, $referenced_language, $items, $form, $form_state);

        // Reset form entity.
        // @see https://www.drupal.org/node/2318109#comment-9068141
        if ($preserved_entity) {
          $form['#entity'] = $preserved_entity;
        }
        $form[$field_name] = reset($field_form);

        // Unset current input to use new default values in form.
        unset($form_input[$field_name]);
      }
    }
  }
}