You are here

function entity_translation_field_attach_form in Entity Translation 7

Implementation of hook_field_attach_form().

File

./entity_translation.module, line 1202

Code

function entity_translation_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {

  // Avoid recursing into the source form.
  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  if (empty($form['#entity_translation_source_form']) && entity_translation_enabled($entity_type, $bundle)) {
    $handler = entity_translation_get_handler($entity_type, $entity);
    $langcode = !empty($langcode) ? $langcode : $handler
      ->getLanguage();
    $form_langcode = $handler
      ->getActiveLanguage();
    $translations = $handler
      ->getTranslations();
    $update_langcode = $form_langcode && $form_langcode != $langcode;
    $source = $handler
      ->getSourceLanguage();
    $new_translation = !isset($translations->data[$form_langcode]);

    // If we are creating a new translation we need to retrieve form elements
    // populated with the source language values, but only if form is not being
    // rebuilt. In this case source values have already been populated, so we
    // need to preserve possible changes. There might be situations, e.g. ajax
    // calls, where the form language has not been properly initialized before
    // calling field_attach_form(). In this case we need to rebuild the form
    // with the correct form language and replace the field elements with the
    // correct ones.
    if ($update_langcode || $source && !isset($translations->data[$form_langcode]) && isset($translations->data[$source]) && empty($form_state['rebuild'])) {
      foreach (field_info_instances($entity_type, $bundle) as $instance) {
        $field_name = $instance['field_name'];
        $field = field_info_field($field_name);

        // If we are creating a new translation we have to change the form item
        // language information from source to target language, this way the
        // user can find the form items already populated with the source values
        // while the field form element holds the correct language information.
        if ($field['translatable'] && isset($form[$field_name])) {
          $element =& $form[$field_name];
          $element['#entity_type'] = $entity_type;
          $element['#entity'] = $entity;
          $element['#entity_id'] = $id;
          $element['#field_name'] = $field_name;
          $element['#source'] = $update_langcode ? $form_langcode : $source;
          $element['#previous'] = NULL;
          $element['#form_parents'] = $form['#parents'];

          // If we are updating the form language we need to make sure that the
          // wrong language is unset and the right one is stored in the field
          // element (see entity_translation_prepare_element()).
          if ($update_langcode) {
            $element['#previous'] = $element['#language'];
            $element['#language'] = $form_langcode;
          }

          // Swap default values during form processing to avoid recursion. We
          // try to act before any other callback so that the correct values are
          // already in place for them.
          if (!isset($element['#process'])) {
            $element['#process'] = array();
          }
          array_unshift($element['#process'], 'entity_translation_prepare_element');
        }
      }
    }

    // Handle fields shared between translations when there is at least one
    // translation available or a new one is being created.
    if (!$handler
      ->isNewEntity() && ($new_translation || count($translations->data) > 1)) {
      $shared_access = $handler
        ->getSharedFieldsAccess();
      list(, , $bundle) = entity_extract_ids($entity_type, $entity);
      foreach (field_info_instances($entity_type, $bundle) as $instance) {
        $field_name = $instance['field_name'];

        // Check if a field is part of the form array.
        if (isset($form[$field_name])) {
          $field = field_info_field($field_name);

          // If access is not set or is granted we check whether the user has
          // access to shared fields.
          $form[$field_name]['#access'] = (!isset($form[$field_name]['#access']) || $form[$field_name]['#access']) && ($field['translatable'] || $shared_access);
          $form[$field_name]['#multilingual'] = (bool) $field['translatable'];
        }
      }
    }

    // If a translation is being created, an alias may be generated if needed.
    // The standard behavior is defaulting to FALSE when an entity already
    // exists, hence we need to override it here.
    // If needed, a variable is provided for reverting to the default behavior.
    if (module_exists('pathauto') && $handler
      ->getSourceLanguage()) {
      $entity->path['pathauto'] = variable_get('entity_translation_pathauto_state_active_new_translation', TRUE);
    }
  }
}