You are here

function entityreference_prepopulate_field_attach_form in Entityreference prepopulate 7

Implements hook_field_attach_form().

File

./entityreference_prepopulate.module, line 64
Prepopulate entity reference values from URL.

Code

function entityreference_prepopulate_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);

  // Check if there is a field that needs to be prepopulated attached to the
  // given entity.
  $found = FALSE;
  foreach (field_info_instances($entity_type, $bundle) as $instance) {
    if (!empty($instance['settings']['behaviors']['prepopulate']['status'])) {
      $found = TRUE;
      break;
    }
  }
  if (!$found) {
    return;
  }
  foreach (element_children($form_state['field']) as $field_name) {
    foreach ($form_state['field'][$field_name] as $lang => $value) {
      $instance = $value['instance'];
      if (empty($instance['settings']['behaviors']['prepopulate']['status'])) {
        continue;
      }
      $settings = $instance['settings']['behaviors']['prepopulate'];
      $field = $value['field'];

      // Store prepopulated values in the form state to make them persistent,
      // in case the form is rebuilt by AJAX requests.
      $field_name = $field['field_name'];
      if ($ids = entityreference_prepopulate_get_values($field, $instance)) {
        $form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name] = $ids;
      }
      if (!empty($settings['skip_perm']) && user_access($settings['skip_perm']) || $id && empty($settings['action_on_edit'])) {

        // User has access to skip the action, or the entity is already
        // saved, but "Apply action on edit", is disabled.
        continue;
      }
      if ($ids || $id && !empty($settings['action_on_edit'])) {

        // New entity with prepopulate values, or an existing entity,
        // we might need to disable/ hide the group-audience field.
        if ($settings['action'] == 'disable') {
          $form[$field_name][$lang]['#disabled'] = TRUE;
        }
        elseif ($settings['action'] == 'hide' && !$id) {

          // For new entities we don't hide the field via hook_field_access(),
          // as the default value won't be set. We use hook_field_access() only
          // on existing ones.
          $form[$field_name]['#access'] = FALSE;
        }
      }
      elseif (in_array($settings['fallback'], array(
        'form_error',
        'redirect',
        'hide',
      ))) {
        $message = t('Field @label must be populated via URL.', array(
          '@label' => $instance['label'],
        ));
        if ($settings['fallback'] == 'form_error') {
          form_error($form, $message);
        }
        elseif ($settings['fallback'] == 'redirect') {
          drupal_set_message($message, 'notice');
          drupal_goto();
        }
        elseif ($settings['fallback'] == 'hide') {
          $form[$field_name]['#access'] = FALSE;
        }
      }
    }
  }
}