diff --git a/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php b/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php index 6c451b8..d21c8cc 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php @@ -179,17 +179,7 @@ public function deleteRevision() { } /** * {@inheritdoc} - * - * @todo Do we need a separate PrepareViewInterface ? */ public static function prepareView(array $entities_items) { } - - // @todo - - /** - * {@inheritdoc} - */ - public function prepareTranslation(EntityInterface $source_entity, $source_langcode) { } - } diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 21b7263..512aa5e 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -522,25 +522,6 @@ 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 \Drupal\Core\Entity\EntityInterface $entity - * The entity being prepared for translation. - * @param $context - * An associative array containing: - * - langcode: The language the entity will be translated to. - * - source_entity: The entity holding the field values to be translated. - * - source_langcode: The source language from which to translate. - */ -function hook_field_attach_prepare_translation_alter(\Drupal\Core\Entity\EntityInterface $entity, $context) { - if ($entity->entityType() == '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 language codes for the diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index 713a21e..862dfa1 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -117,9 +117,6 @@ /** * Invokes a method on all the fields of a given entity. * - * @todo Remove _field_invoke() and friends when field types and formatters are - * turned into plugins. - * * @param string $method * The name of the method to invoke. * @param callable $target_function @@ -338,134 +335,21 @@ function field_invoke_method_multiple($method, $target_function, array $entities } /** - * Invoke a field hook. - * - * @param $op - * Possible operations include: - * - form - * - validate - * - presave - * - insert - * - update - * - delete - * - delete revision - * - view - * - prepare translation - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity object. - * @param $a - * - The $form in the 'form' operation. - * - The value of $view_mode in the 'view' operation. - * - Otherwise NULL. - * @param $b - * - The $form_state in the 'submit' operation. - * - Otherwise NULL. - * @param $options - * An associative array of additional options, with the following keys: - * - 'field_name': The name of the field whose operation should be - * invoked. By default, the operation is invoked on all the fields - * in the entity's bundle. NOTE: This option is not compatible with - * the 'deleted' option; the 'field_id' option should be used - * instead. - * - 'field_id': The id of the field whose operation should be - * invoked. By default, the operation is invoked on all the fields - * in the entity's' bundles. - * - 'default': A boolean value, specifying which implementation of - * the operation should be invoked. - * - if FALSE (default), the field types implementation of the operation - * will be invoked (hook_field_[op]) - * - If TRUE, the default field implementation of the field operation - * will be invoked (field_default_[op]) - * Internal use only. Do not explicitely set to TRUE, but use - * _field_invoke_default() instead. - * - 'deleted': If TRUE, the function will operate on deleted fields - * as well as non-deleted fields. If unset or FALSE, only - * non-deleted fields are operated on. - * - 'langcode': A language code or an array of language codes keyed by field - * name. It will be used to narrow down to a single value the available - * languages to act on. - */ -function _field_invoke($op, EntityInterface $entity, &$a = NULL, &$b = NULL, $options = array()) { - // Merge default options. - $default_options = array( - 'default' => FALSE, - 'deleted' => FALSE, - 'langcode' => NULL, - ); - $options += $default_options; - - // Determine the list of instances to iterate on. - $instances = _field_invoke_get_instances($entity->entityType(), $entity->bundle(), $options); - - // Iterate through the instances and collect results. - $return = array(); - foreach ($instances as $instance) { - // field_info_field() is not available for deleted fields, so use - // field_info_field_by_id(). - $field = field_info_field_by_id($instance['field_id']); - $field_name = $field['field_name']; - $function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op; - if (function_exists($function)) { - // Determine the list of languages to iterate on. - $available_langcodes = field_available_languages($entity->entityType(), $field); - $langcodes = _field_language_suggestion($available_langcodes, $options['langcode'], $field_name); - - foreach ($langcodes as $langcode) { - $items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array(); - $result = $function($entity, $field, $instance, $langcode, $items, $a, $b); - if (isset($result)) { - // For hooks with array results, we merge results together. - // For hooks with scalar results, we collect results in an array. - if (is_array($result)) { - $return = array_merge($return, $result); - } - else { - $return[] = $result; - } - } - - // Populate $items back in the field values, but avoid replacing missing - // fields with an empty array (those are not equivalent on update). - if ($items !== array() || isset($entity->{$field_name}[$langcode])) { - $entity->{$field_name}[$langcode] = $items; - } - } - } - } - - return $return; -} - -/** - * Invoke field.module's version of a field hook. - * - * This function invokes the field_default_[op]() function. - * Use _field_invoke() to invoke the field type implementation, - * hook_field_[op](). - * - * @see _field_invoke() - */ -function _field_invoke_default($op, EntityInterface $entity, &$a = NULL, &$b = NULL, $options = array()) { - $options['default'] = TRUE; - return _field_invoke($op, $entity, $a, $b, $options); -} - -/** * Retrieves a list of instances to operate on. * - * Helper for _field_invoke(). + * Helper for field_invoke_method(). * * @param $entity_type * The entity type. * @param $bundle * The bundle name. * @param $options - * An associative array of options, as provided to _field_invoke(). Only the - * following keys are considered: + * An associative array of options, as provided to field_invoke_method(). Only + * the following keys are considered: * - deleted * - field_name * - field_id - * See _field_invoke() for details. + * See field_invoke_method() for details. * * @return * The array of selected instance definitions. @@ -1260,45 +1144,6 @@ function field_attach_preprocess(EntityInterface $entity, $element, &$variables) } /** - * Prepares an entity for translation. - * - * 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 \Drupal\Core\Entity\EntityInterface $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(EntityInterface $entity, $langcode, EntityInterface $source_entity, $source_langcode) { - // Ensure we are working with a BC mode entity. - $entity = $entity->getBCEntity(); - - $options = array('langcode' => $langcode); - // Copy source field values into the entity to be prepared. - _field_invoke_default('prepare_translation', $entity, $source_entity, $source_langcode, $options); - // Let field types handle their own advanced translation pattern if needed. - _field_invoke('prepare_translation', $entity, $source_entity, $source_langcode, $options); - // Let other modules alter the entity translation. - $context = array( - 'langcode' => $langcode, - 'source_entity' => $source_entity, - 'source_langcode' => $source_langcode, - ); - drupal_alter('field_attach_prepare_translation', $entity, $context); -} - -/** * Implements hook_entity_bundle_create(). */ function field_entity_bundle_create($entity_type, $bundle) { diff --git a/core/modules/field/field.multilingual.inc b/core/modules/field/field.multilingual.inc index 17c5908..c04c218 100644 --- a/core/modules/field/field.multilingual.inc +++ b/core/modules/field/field.multilingual.inc @@ -34,9 +34,9 @@ * property returned by field_info_field() and whether the entity type the field * is attached to supports translation. * - * By default, _field_invoke() processes a field in all available languages, - * unless they are given a language code suggestion. Based on that suggestion, - * _field_language_suggestion() determines the languages to act on. + * By default, field_invoke_method() processes a field in all available + * languages, unless they are given a language code suggestion. Based on that + * suggestion, _field_language_suggestion() determines the languages to act on. * * Most field_attach_*() functions act on all available language codes, except * for the following: diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php index 0ab0974..f425846 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php @@ -67,23 +67,4 @@ public function getConstraints() { return $constraints; } - // @todo... former code in field.default.inc - - /** - * {@inheritdoc} - */ - public function prepareTranslation(EntityInterface $source_entity, $source_langcode) { - $field = $this->field; - - // @todo Adapt... - - // If the field is untranslatable keep using LANGCODE_NOT_SPECIFIED. - if ($langcode == Language::LANGCODE_NOT_SPECIFIED) { - $source_langcode = Language::LANGCODE_NOT_SPECIFIED; - } - if (isset($source_entity->{$field->id}[$source_langcode])) { - $items = $source_entity->{$field->id}[$source_langcode]; - } - } - } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php index f71773e..c79858e 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php @@ -101,8 +101,6 @@ public function instanceSettingsForm(array $form, array &$form_state); /** * Prepares field values prior to display. * - * @todo - * * This method is invoked before the field values are handed to formatters * for display. * @@ -115,20 +113,4 @@ public function instanceSettingsForm(array $form, array &$form_state); */ public static function prepareView(array $entities_items); - // @todo Decide what to do with those - - - /** - * Defines custom translation preparation behavior for field values. - * - * This mathod is called from field_attach_prepare_translation(), during the - * process of preparing an entity for translation in a different language. - * - * @param \Drupal\Core\Entity\EntityInterface $source_entity - * The source entity from which field values are being copied. - * @param string $source_langcode - * The source language from which field values are being copied. - */ - public function prepareTranslation(EntityInterface $source_entity, $source_langcode); - } diff --git a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php index a1780e7..ca37842 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php +++ b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php @@ -125,17 +125,4 @@ protected function legacyCallback($hook, $args = array()) { } } - - // @todo - what's below is not working nor actually invoked. - - /** - * {@inheritdoc} - */ - public function prepareTranslation(EntityInterface $source_entity, $source_langcode) { -// parent::prepareTranslation($source_entity, $source_langcode); -// if ($callback = $this->legacyCallback('prepare_translation')) { -// $callback($entity->entityType(), $entity, $this->field, $instance, $langcode, $items, $source_entity, $source_langcode); -// } - } - } diff --git a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php index 0ee43cf..9e97e34 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php @@ -12,7 +12,7 @@ /** * Unit test class for the multilanguage fields logic. * - * The following tests will check the multilanguage logic of _field_invoke() and + * The following tests will check the multilanguage logic in field handling, and * that only the correct values are returned by field_available_languages(). */ class TranslationTest extends FieldUnitTestBase { @@ -101,44 +101,6 @@ function testFieldAvailableLanguages() { } /** - * Test the multilanguage logic of _field_invoke(). - */ - function testFieldInvoke() { - // Enable field translations for the entity. - field_test_entity_info_translatable('test_entity', TRUE); - - $entity_type = 'test_entity'; - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); - - // Populate some extra languages to check if _field_invoke() correctly uses - // the result of field_available_languages(). - $values = array(); - $extra_langcodes = mt_rand(1, 4); - $langcodes = $available_langcodes = field_available_languages($this->entity_type, $this->field); - for ($i = 0; $i < $extra_langcodes; ++$i) { - $langcodes[] = $this->randomName(2); - } - - // For each given language provide some random values. - foreach ($langcodes as $langcode) { - for ($delta = 0; $delta < $this->field['cardinality']; $delta++) { - $values[$langcode][$delta]['value'] = mt_rand(1, 127); - } - } - $entity->{$this->field_name} = $values; - - $results = _field_invoke('test_op', $entity); - foreach ($results as $langcode => $result) { - $hash = hash('sha256', serialize(array($entity, $this->field_name, $langcode, $values[$langcode]))); - // Check whether the parameters passed to _field_invoke() were correctly - // forwarded to the callback function. - $this->assertEqual($hash, $result, format_string('The result for %language is correctly stored.', array('%language' => $langcode))); - } - - $this->assertEqual(count($results), count($available_langcodes), 'No unavailable language has been processed.'); - } - - /** * Test translatable fields storage/retrieval. */ function testTranslatableFieldSaveLoad() { diff --git a/core/modules/field/tests/modules/field_test/field_test.module b/core/modules/field/tests/modules/field_test/field_test.module index a2e422b..67f3229 100644 --- a/core/modules/field/tests/modules/field_test/field_test.module +++ b/core/modules/field/tests/modules/field_test/field_test.module @@ -75,15 +75,6 @@ function field_test_menu() { } /** - * Generic op to test _field_invoke behavior. - * - * This simulates a field operation callback to be invoked by _field_invoke(). - */ -function field_test_field_test_op(EntityInterface $entity, $field, $instance, $langcode, &$items) { - return array($langcode => hash('sha256', serialize(array($entity, $field['field_name'], $langcode, $items)))); -} - -/** * Implements hook_field_available_languages_alter(). */ function field_test_field_available_languages_alter(&$langcodes, $context) { diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItemBase.php b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItemBase.php index 55ccb4a..27ce13e 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItemBase.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItemBase.php @@ -122,25 +122,4 @@ public function prepareCache() { // } // } - // @todo - - /** - * {@inheritdoc} - */ - public function prepareTranslation(EntityInterface $source_entity, $source_langcode) { - parent::prepareTranslation($entity, $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. - if (!empty($source_entity->{$this->field->id}[$source_langcode])) { - $formats = filter_formats(); - foreach ($source_entity->{$this->field->id}[$source_langcode] as $delta => $item) { - $format_id = $item['format']; - if (!empty($format_id) && !filter_access($formats[$format_id])) { - unset($items[$delta]); - } - } - } - } - } diff --git a/core/modules/text/lib/Drupal/text/Tests/TextTranslationTest.php b/core/modules/text/lib/Drupal/text/Tests/TextTranslationTest.php deleted file mode 100644 index 58be48f..0000000 --- a/core/modules/text/lib/Drupal/text/Tests/TextTranslationTest.php +++ /dev/null @@ -1,137 +0,0 @@ - 'Text translation', -// 'description' => 'Check if the text field is correctly prepared for translation.', -// 'group' => 'Field types', -// ); -// } - - function setUp() { - parent::setUp(); - - $full_html_format = filter_format_load('full_html'); - $this->format = $full_html_format->format; - $this->admin = $this->drupalCreateUser(array( - 'administer languages', - 'administer content types', - 'administer node fields', - 'access administration pages', - 'bypass node access', - filter_permission_name($full_html_format), - )); - $this->translator = $this->drupalCreateUser(array('create article content', 'edit own article content', 'translate all content')); - - // Enable an additional language. - $this->drupalLogin($this->admin); - $edit = array('langcode' => 'fr'); - $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); - - // Set "Article" content type to use multilingual support with translation. - $edit = array('language_configuration[language_show]' => TRUE, 'node_type_language_translation_enabled' => TRUE); - $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type')); - $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Article')), 'Article content type has been updated.'); - } - - /** - * Test that a plaintext textfield widget is correctly populated. - */ - function testTextField() { - // Disable text processing for body. - $edit = array('instance[settings][text_processing]' => 0); - $this->drupalPost('admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings')); - - // Login as translator. - $this->drupalLogin($this->translator); - - // Create content. - $langcode = Language::LANGCODE_NOT_SPECIFIED; - $body = $this->randomName(); - $edit = array( - 'title' => $this->randomName(), - 'langcode' => 'en', - "body[$langcode][0][value]" => $body, - ); - - // Translate the article in french. - $this->drupalPost('node/add/article', $edit, t('Save')); - $node = $this->drupalGetNodeByTitle($edit['title']); - $this->drupalGet("node/$node->nid/translate"); - $this->clickLink(t('Add translation')); - $this->assertFieldByXPath("//textarea[@name='body[$langcode][0][value]']", $body, 'The textfield widget is populated.'); - } - - /** - * Check that user that does not have access the field format cannot see the - * source value when creating a translation. - */ - function testTextFieldFormatted() { - // Make node body multiple. - $edit = array('field[cardinality]' => FIELD_CARDINALITY_UNLIMITED); - $this->drupalPost('admin/structure/types/manage/article/fields/node.article.body/field', $edit, t('Save field settings')); - $this->drupalGet('node/add/article'); - $this->assertFieldByXPath("//input[@name='body_add_more']", t('Add another item'), 'Body field cardinality set to multiple.'); - - $body = array( - $this->randomName(), - $this->randomName(), - ); - - // Create an article with the first body input format set to "Full HTML". - $title = $this->randomName(); - $edit = array( - 'title' => $title, - 'langcode' => 'en', - ); - $this->drupalPost('node/add/article', $edit, t('Save')); - - // Populate the body field: the first item gets the "Full HTML" input - // format, the second one "Basic HTML". - $formats = array('full_html', 'basic_html'); - $langcode = Language::LANGCODE_NOT_SPECIFIED; - foreach ($body as $delta => $value) { - $edit = array( - "body[$langcode][$delta][value]" => $value, - "body[$langcode][$delta][format]" => array_shift($formats), - ); - $this->drupalPost('node/1/edit', $edit, t('Save')); - $this->assertText($body[$delta], t('The body field with delta @delta has been saved.', array('@delta' => $delta))); - } - - // Login as translator. - $this->drupalLogin($this->translator); - - // Translate the article in french. - $node = $this->drupalGetNodeByTitle($title); - $this->drupalGet("node/$node->nid/translate"); - $this->clickLink(t('Add translation')); - $this->assertNoText($body[0], t('The body field with delta @delta is hidden.', array('@delta' => 0))); - $this->assertText($body[1], t('The body field with delta @delta is shown.', array('@delta' => 1))); - } -} diff --git a/core/modules/translation/lib/Drupal/translation/Tests/TranslationTest.php b/core/modules/translation/lib/Drupal/translation/Tests/TranslationTest.php index 12ac64e..dff34a0 100644 --- a/core/modules/translation/lib/Drupal/translation/Tests/TranslationTest.php +++ b/core/modules/translation/lib/Drupal/translation/Tests/TranslationTest.php @@ -25,14 +25,13 @@ class TranslationTest extends WebTestBase { protected $book; -// @todo Uncomment when field_attach_prepare_translation() works again. -// public static function getInfo() { -// return array( -// 'name' => 'Translation functionality', -// 'description' => 'Create a basic page with translation, modify the page outdating translation, and update translation.', -// 'group' => 'Translation' -// ); -// } + public static function getInfo() { + return array( + 'name' => 'Translation functionality', + 'description' => 'Create a basic page with translation, modify the page outdating translation, and update translation.', + 'group' => 'Translation' + ); + } function setUp() { parent::setUp(); @@ -381,7 +380,6 @@ function createTranslation(EntityInterface $node, $title, $body, $langcode) { $field_langcode = Language::LANGCODE_NOT_SPECIFIED; $body_key = "body[$field_langcode][0][value]"; $this->assertFieldByXPath('//input[@id="edit-title"]', $node->label(), "Original title value correctly populated."); - $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[Language::LANGCODE_NOT_SPECIFIED][0]['value'], "Original body value correctly populated."); $edit = array(); $edit["title"] = $title; diff --git a/core/modules/translation/translation.module b/core/modules/translation/translation.module index 115be1a..e8e872e 100644 --- a/core/modules/translation/translation.module +++ b/core/modules/translation/translation.module @@ -341,10 +341,6 @@ function translation_node_prepare(EntityInterface $node) { $node->langcode = $langcode; $node->translation_source = $source_node; $node->title = $source_node->title; - - // Add field translations and let other modules module add custom translated - // fields. - field_attach_prepare_translation($node, $node->langcode, $source_node, $source_node->langcode); } }