diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-06-06 00:24:16 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-06-06 00:24:16 +0000 |
commit | cf30c78b42058488fb8fb4f535a469285ca31a31 (patch) | |
tree | 4a26806c2eaead66fcf39f3f0665916d5a333a89 /modules/field | |
parent | 8fb6adbc21302e896b1019aad2edd5c87a518a98 (diff) | |
download | brdo-cf30c78b42058488fb8fb4f535a469285ca31a31.tar.gz brdo-cf30c78b42058488fb8fb4f535a469285ca31a31.tar.bz2 |
#362021 by plach, yched, sun: Update field_attach_prepare_translation() for D7 entity API.
Diffstat (limited to 'modules/field')
-rw-r--r-- | modules/field/field.api.php | 38 | ||||
-rw-r--r-- | modules/field/field.attach.inc | 49 | ||||
-rw-r--r-- | modules/field/field.default.inc | 33 | ||||
-rw-r--r-- | modules/field/modules/text/text.module | 13 |
4 files changed, 109 insertions, 24 deletions
diff --git a/modules/field/field.api.php b/modules/field/field.api.php index f43322c75..cba0957bd 100644 --- a/modules/field/field.api.php +++ b/modules/field/field.api.php @@ -564,8 +564,6 @@ function hook_field_delete_revision($entity_type, $entity, $field, $instance, $l /** * Define custom prepare_translation behavior for this module's field types. * - * TODO: This hook may or may not survive in Field API. - * * @param $entity_type * The type of $entity. * @param $entity @@ -578,8 +576,20 @@ function hook_field_delete_revision($entity_type, $entity, $field, $instance, $l * The language associated to $items. * @param $items * $entity->{$field['field_name']}[$langcode], or an empty array if unset. - */ -function hook_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items) { + * @param $source_entity + * The source entity from which field values are being copied. + * @param $source_langcode + * The source language from which field values are being copied. + */ +function hook_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) { + // If the translating user is not permitted to use the assigned text format, + // we must not expose the source values. + $field_name = $field['field_name']; + $formats = filter_formats(); + $format_id = $source_entity->{$field_name}[$source_langcode][0]['format']; + if (!filter_access($formats[$format_id])) { + $items = array(); + } } /** @@ -1180,6 +1190,26 @@ function hook_field_attach_view_alter(&$output, $context) { } /** + * Perform alterations on field_attach_prepare_translation(). + * + * This hook is invoked after the field module has performed the operation. + * + * @param &$entity + * The entity being prepared for translation. + * @param $context + * An associative array containing: + * - entity_type: The type of $entity; e.g. 'node' or 'user'. + * - langcode: The language the entity has to be translated in. + * - source_entity: The entity holding the field values to be translated. + * - source_langcode: The source language from which translate. + */ +function hook_field_attach_prepare_translation_alter(&$entity, $context) { + if ($context['entity_type'] == 'custom_entity_type') { + $entity->custom_field = $context['source_entity']->custom_field; + } +} + +/** * Perform alterations on field_language() values. * * This hook is invoked to alter the array of display languages for the given diff --git a/modules/field/field.attach.inc b/modules/field/field.attach.inc index 8a7991d41..b1f6a57e0 100644 --- a/modules/field/field.attach.inc +++ b/modules/field/field.attach.inc @@ -1325,23 +1325,42 @@ function field_attach_preprocess($entity_type, $entity, $element, &$variables) { } /** - * Implements hook_node_prepare_translation(). + * Prepares an entity for translation. * - * TODO D7: We do not yet know if this really belongs in Field API. + * This function is used to fill-in the form default values for Field API fields + * while performing entity translation. By default it copies all the source + * values in the given source language to the new entity and assigns them the + * target language. + * + * This is used as part of the 'per entity' translation pattern, which is + * implemented only for nodes by translation.module. Other entity types may be + * supported through contributed modules. + * + * @param $entity_type + * The type of $entity; e.g. 'node' or 'user'. + * @param $entity + * The entity to be prepared for translation. + * @param $langcode + * The language the entity has to be translated in. + * @param $source_entity + * The source entity holding the field values to be translated. + * @param $source_langcode + * The source language from which translate. */ -function field_attach_prepare_translation($node) { - // Prevent against invalid 'nodes' built by broken 3rd party code. - if (isset($node->type)) { - $type = content_types($node->type); - // Save cycles if the type has no fields. - if (!empty($type['instances'])) { - $default_additions = _field_invoke_default('prepare_translation', $node); - $additions = _field_invoke('prepare_translation', $node); - // Merge module additions after the default ones to enable overriding - // of field values. - $node = (object) array_merge((array) $node, $default_additions, $additions); - } - } +function field_attach_prepare_translation($entity_type, $entity, $langcode, $source_entity, $source_langcode) { + $options = array('language' => $langcode); + // Copy source field values into the entity to be prepared. + _field_invoke_default('prepare_translation', $entity_type, $entity, $source_entity, $source_langcode, $options); + // Let field types handle their own advanced translation pattern if needed. + _field_invoke('prepare_translation', $entity_type, $entity, $source_entity, $source_langcode, $options); + // Let other modules alter the entity translation. + $context = array( + 'entity_type' => $entity_type, + 'langcode' => $langcode, + 'source_entity' => $source_entity, + 'source_langcode' => $source_langcode, + ); + drupal_alter('field_attach_prepare_translation', $entity, $context); } /** diff --git a/modules/field/field.default.inc b/modules/field/field.default.inc index eebde1e14..4cd512392 100644 --- a/modules/field/field.default.inc +++ b/modules/field/field.default.inc @@ -226,10 +226,33 @@ function field_default_view($entity_type, $entity, $field, $instance, $langcode, return $addition; } -function field_default_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items) { - $addition = array(); - if (isset($entity->translation_source->$field['field_name'])) { - $addition[$field['field_name']] = $entity->translation_source->$field['field_name']; +/** + * Copies source field values into the entity to be prepared. + * + * @param $entity_type + * The type of $entity; e.g. 'node' or 'user'. + * @param $entity + * The entity to be prepared for translation. + * @param $field + * The field structure for the operation. + * @param $instance + * The instance structure for $field on $entity's bundle. + * @param $langcode + * The language the entity has to be translated in. + * @param $items + * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * @param $source_entity + * The source entity holding the field values to be translated. + * @param $source_langcode + * The source language from which translate. + */ +function field_default_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) { + $field_name = $field['field_name']; + // If the field is untranslatable keep using LANGUAGE_NONE. + if ($langcode == LANGUAGE_NONE) { + $source_langcode = LANGUAGE_NONE; + } + if (isset($source_entity->{$field_name}[$source_langcode])) { + $items = $source_entity->{$field_name}[$source_langcode]; } - return $addition; } diff --git a/modules/field/modules/text/text.module b/modules/field/modules/text/text.module index 47d41ab0d..05a286e54 100644 --- a/modules/field/modules/text/text.module +++ b/modules/field/modules/text/text.module @@ -590,3 +590,16 @@ function text_field_widget_error($element, $error, $form, &$form_state) { form_error($error_element, $error['message']); } +/** + * Implements hook_field_prepare_translation(). + */ +function text_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) { + // If the translating user is not permitted to use the assigned text format, + // we must not expose the source values. + $field_name = $field['field_name']; + $formats = filter_formats(); + $format_id = $source_entity->{$field_name}[$source_langcode][0]['format']; + if (!filter_access($formats[$format_id])) { + $items = array(); + } +} |