diff -u b/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php --- b/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -484,38 +484,37 @@ /** * {@inheritdoc} */ - public function getExtraFields($entity_type, $bundle) { + public function getExtraFields($entity_type_id, $bundle) { // Read from the "static" cache. - if (isset($this->bundleExtraFields[$entity_type][$bundle])) { - return $this->bundleExtraFields[$entity_type][$bundle]; + if (isset($this->bundleExtraFields[$entity_type_id][$bundle])) { + return $this->bundleExtraFields[$entity_type_id][$bundle]; } // Read from the persistent cache. Since hook_entity_extra_field_info() and // hook_entity_extra_field_info_alter() might contain t() calls, we cache // per language. - $langcode = $this->languageManager->getCurrentLanguage()->id; - $cache_id = "entity_bundle_extra_fields:$langcode:$entity_type:$bundle"; + $cache_id = 'entity_bundle_extra_fields:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->id; $cached = $this->cache->get($cache_id); if ($cached) { - $this->bundleExtraFields[$entity_type][$bundle] = $cached->data; - return $this->bundleExtraFields[$entity_type][$bundle]; + $this->bundleExtraFields[$entity_type_id][$bundle] = $cached->data; + return $this->bundleExtraFields[$entity_type_id][$bundle]; } $extra = $this->moduleHandler->invokeAll('entity_extra_field_info'); $this->moduleHandler->alter('entity_extra_field_info', $extra); - $info = isset($extra[$entity_type][$bundle]) ? $extra[$entity_type][$bundle] : array(); + $info = isset($extra[$entity_type_id][$bundle]) ? $extra[$entity_type_id][$bundle] : array(); $info += array( 'form' => array(), 'display' => array(), ); // Store in the 'static' and persistent caches. - $this->bundleExtraFields[$entity_type][$bundle] = $info; + $this->bundleExtraFields[$entity_type_id][$bundle] = $info; $this->cache->set($cache_id, $info, Cache::PERMANENT, array( 'entity_field_info' => TRUE, )); - return $this->bundleExtraFields[$entity_type][$bundle]; + return $this->bundleExtraFields[$entity_type_id][$bundle]; } /** diff -u b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php --- b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php @@ -189,15 +189,31 @@ /** * Retrieves the "extra fields" for a bundle. * - * @param string $entity_type - * The entity type. + * @param string $entity_type_id + * The entity type ID. * @param string $bundle * The bundle name. * * @return array - * The array of extra fields. + * A nested array of 'pseudo-field' elements. Each list is nested within the + * following keys: entity type, bundle name, context (either 'form' or + * 'display'). The keys are the name of the elements as appearing in the + * renderable array (either the entity form or the displayed entity). The + * value is an associative array: + * - label: The human readable name of the element. Make sure you sanitize + * this appropriately. + * - description: A short description of the element contents. + * - weight: The default weight of the element. + * - visible: (optional) The default visibility of the element. Defaults to + * TRUE. + * - edit: (optional) String containing markup (normally a link) used as the + * element's 'edit' operation in the administration interface. Only for + * 'form' context. + * - delete: (optional) String containing markup (normally a link) used as the + * element's 'delete' operation in the administration interface. Only for + * 'form' context. */ - public function getExtraFields($entity_type, $bundle); + public function getExtraFields($entity_type_id, $bundle); /** * Returns the entity translation to be used in the given context. diff -u b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php --- b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php @@ -363,6 +363,41 @@ ); } + /** + * Tests that the extra fields can be translated. + */ + function testFieldInfoExtraFieldsTranslation() { + $this->enableModules(array('language', 'locale')); + $this->installSchema('locale', array('locales_source', 'locales_target', 'locales_location')); + foreach (array('en', 'hu') as $id) { + $language = new Language(array( + 'id' => $id, + )); + language_save($language); + } + $locale_storage = $this->container->get('locale.storage'); + + // Create test source string. + $en_string = $locale_storage->createString(array( + 'source' => 'User name and password', + 'context' => '', + ))->save(); + + // Create translation for new string and save it. + $translated_string = $this->randomString(); + $locale_storage->createTranslation(array( + 'lid' => $en_string->lid, + 'language' => 'hu', + 'translation' => $translated_string, + ))->save(); + + // Check that the label is translated. + \Drupal::translation()->setDefaultLangcode('hu'); + $field_info = \Drupal::service('field.info'); + $user_fields = $field_info->getBundleExtraFields('user', 'user'); + $this->assertEqual($user_fields['form']['account']['label'], $translated_string); + } + } $translated_string = $this->randomString(); $locale_storage->createTranslation(array( @@ -380,2 +415,37 @@ + /** + * Tests that the extra fields can be translated. + */ + function testFieldInfoExtraFieldsTranslation() { + $this->enableModules(array('language', 'locale')); + $this->installSchema('locale', array('locales_source', 'locales_target', 'locales_location')); + foreach (array('en', 'hu') as $id) { + $language = new Language(array( + 'id' => $id, + )); + language_save($language); + } + $locale_storage = $this->container->get('locale.storage'); + + // Create test source string. + $en_string = $locale_storage->createString(array( + 'source' => 'User name and password', + 'context' => '', + ))->save(); + + // Create translation for new string and save it. + $translated_string = $this->randomString(); + $locale_storage->createTranslation(array( + 'lid' => $en_string->lid, + 'language' => 'hu', + 'translation' => $translated_string, + ))->save(); + + // Check that the label is translated. + \Drupal::translation()->setDefaultLangcode('hu'); + $field_info = \Drupal::service('field.info'); + $user_fields = $field_info->getBundleExtraFields('user', 'user'); + $this->assertEqual($user_fields['form']['account']['label'], $translated_string); + } + } diff -u b/core/modules/system/entity.api.php b/core/modules/system/entity.api.php --- b/core/modules/system/entity.api.php +++ b/core/modules/system/entity.api.php @@ -820,7 +820,6 @@ * (\Drupal\Core\Field\FieldItemListInterface). */ function hook_entity_field_access_alter(array &$grants, array $context) { - /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ $field_definition = $context['field_definition']; if ($field_definition->getName() == 'field_of_interest' && $grants['node'] === FALSE) { // Override node module's restriction to no opinion. We don't want to @@ -843,6 +842,7 @@ * (\Drupal\Core\Field\FieldItemListInterface). */ function hook_entity_field_access_alter(array &$grants, array $context) { + /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ $field_definition = $context['field_definition']; if ($field_definition->getName() == 'field_of_interest' && $grants['node'] === FALSE) { // Override node module's restriction to no opinion. We don't want to @@ -855,38 +855,22 @@ } /** - * Exposes "pseudo-field" components on fieldable entities. + * Exposes "pseudo-field" components on content entities. * * Field UI's "Manage fields" and "Manage display" pages let users re-order * fields, but also non-field components. For nodes, these include the title * and other elements exposed by modules through hook_form_alter(). * - * Fieldable entities or modules that want to have their components supported + * Content entities or modules that want to have their components supported * should expose them using this hook. The user-defined settings (weight, - * visible) are automatically applied on rendered forms and displayed entities - * in a #pre_render callback added by field_attach_form() and - * EntityViewBuilder::viewMultiple(). + * visible) are automatically applied when entities or entity forms are + * rendered. * * @see hook_entity_extra_field_info_alter() * * @return array - * A nested array of 'pseudo-field' elements. Each list is nested within the - * following keys: entity type, bundle name, context (either 'form' or - * 'display'). The keys are the name of the elements as appearing in the - * renderable array (either the entity form or the displayed entity). The - * value is an associative array: - * - label: The human readable name of the element. Make sure you sanitize - * this appropriately. - * - description: A short description of the element contents. - * - weight: The default weight of the element. - * - visible: (optional) The default visibility of the element. Defaults to - * TRUE. - * - edit: (optional) String containing markup (normally a link) used as the - * element's 'edit' operation in the administration interface. Only for - * 'form' context. - * - delete: (optional) String containing markup (normally a link) used as the - * element's 'delete' operation in the administration interface. Only for - * 'form' context. + * The array structure is identical to that of the return value of + * \Drupal\Core\Entity\EntityManagerInterface::getExtraFields(). */ function hook_entity_extra_field_info() { $extra = array(); @@ -930,8 +914,9 @@ /** * Alter "pseudo-field" components on fieldable entities. * - * @param $info - * The associative array of 'pseudo-field' components. + * @param array $info + * The array structure is identical to that of the return value of + * \Drupal\Core\Entity\EntityManagerInterface::getExtraFields(). * * @see hook_entity_extra_field_info() */ diff -u b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php --- b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -786,11 +786,11 @@ function testgetExtraFields() { $this->setUpEntityManager(); - $entity_type = $this->randomName(); + $entity_type_id = $this->randomName(); $bundle = $this->randomName(); $language_code = 'en'; $hook_bundle_extra_fields = array( - $entity_type => array( + $entity_type_id => array( $bundle => array( 'form' => array( 'foo_extra_field' => array( @@ -801,10 +801,10 @@ ), ); $processed_hook_bundle_extra_fields = $hook_bundle_extra_fields; - $processed_hook_bundle_extra_fields[$entity_type][$bundle] += array( + $processed_hook_bundle_extra_fields[$entity_type_id][$bundle] += array( 'display' => array(), ); - $cache_id = "entity_bundle_extra_fields:$language_code:$entity_type:$bundle"; + $cache_id = 'entity_bundle_extra_fields:' . $entity_type_id . ':' . $bundle . ':' . $language_code; $language = new Language(); $language->id = $language_code; @@ -827,9 +827,9 @@ $this->cache->expects($this->once()) ->method('set') - ->with($cache_id, $processed_hook_bundle_extra_fields[$entity_type][$bundle]); + ->with($cache_id, $processed_hook_bundle_extra_fields[$entity_type_id][$bundle]); - $this->assertSame($processed_hook_bundle_extra_fields[$entity_type][$bundle], $this->entityManager->getExtraFields($entity_type, $bundle)); + $this->assertSame($processed_hook_bundle_extra_fields[$entity_type_id][$bundle], $this->entityManager->getExtraFields($entity_type_id, $bundle)); } /**