diff -u b/address.module b/address.module --- b/address.module +++ b/address.module @@ -24,7 +24,8 @@ 'locality' => [], 'administrative_area' => [], 'country' => [], - 'element' => [], + 'address' => NULL, + 'view_mode' => '', ], ], ]; @@ -34,18 +35,20 @@ * Implements hook_theme_suggestions_HOOK(). */ function address_theme_suggestions_address_plain(array $variables) { - $original = $variables['theme_hook_original']; - $address = $variables['element']['#address']; - $item = $address->getParent(); - $entity = $item->getParent()->getValue(); - $field = $address->getFieldDefinition(); - $suggestions = []; - $suggestions[] = $original . '__' . $field->getName(); - $suggestions[] = $original . '__' . $entity->getEntityTypeID() . '__' . $entity->bundle(); - $suggestions[] = $original . '__' . $entity->getEntityTypeID() . '__' . $entity->bundle() . '__' . $variables['element']['#view_mode']; - $suggestions[] = $original . '__' . $entity->getEntityTypeID() . '__' . $field->getName(); - $suggestions[] = $original . '__' . $entity->getEntityTypeID() . '__' . $field->getName() . '__' . $entity->bundle(); + $original = $variables['theme_hook_original'] . '__'; + $field = $variables['address']->getFieldDefinition(); + $entity_type_id = $field->getTargetEntityTypeId(); + $bundle = $field->getTargetBundle(); + $sanitized_view_mode = strtr($variables['view_mode'], '.', '_'); + + $suggestions[] = $original . $entity_type_id . '__' . $sanitized_view_mode; + $suggestions[] = $original . $entity_type_id . '__' . $bundle; + $suggestions[] = $original . $entity_type_id . '__' . $bundle . '__' . $sanitized_view_mode; + $suggestions[] = $original . $field->getName(); + $suggestions[] = $original . $entity_type_id . '__' . $field->getName(); + $suggestions[] = $original . $entity_type_id . '__' . $field->getName() . '__' . $bundle; + return $suggestions; } diff -u b/src/Plugin/Field/FieldFormatter/AddressPlainFormatter.php b/src/Plugin/Field/FieldFormatter/AddressPlainFormatter.php --- b/src/Plugin/Field/FieldFormatter/AddressPlainFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddressPlainFormatter.php @@ -51,13 +51,6 @@ protected $subdivisionRepository; /** - * The view mode. - * - * @var \CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface - */ - protected $viewMode; - - /** * Constructs an AddressPlainFormatter object. * * @param string $plugin_id @@ -87,7 +80,6 @@ $this->addressFormatRepository = $address_format_repository; $this->countryRepository = $country_repository; $this->subdivisionRepository = $subdivision_repository; - $this->viewMode = $view_mode; } /** @@ -155,10 +147,8 @@ 'code' => $country_code, 'name' => $countries[$country_code], ], - '#element' => [ - '#address' => $address, - '#view_mode' => $this->viewMode, - ], + '#address' => $address, + '#view_mode' => $this->viewMode, '#cache' => [ 'contexts' => [ 'languages:' . LanguageInterface::TYPE_INTERFACE, only in patch2: unchanged: --- a/templates/address-plain.html.twig +++ b/templates/address-plain.html.twig @@ -24,6 +24,10 @@ * - country: The country. * - country.code: Country code. * - country.name: Country name. + * - address: An object that implements \Drupal\address\AddressInterface + * representing the address to be rendered. Only public methods can be used. + * - view_mode: View mode of the entity being rendered that this address field + * is attached to. For example, "teaser" or "full". * * if a subdivision (dependent_locality, locality, administrative_area) was * entered, the array will always have a code. If it's a predefined subdivision, only in patch2: unchanged: --- a/tests/src/Kernel/Formatter/AddressPlainFormatterTest.php +++ b/tests/src/Kernel/Formatter/AddressPlainFormatterTest.php @@ -12,26 +12,35 @@ use Drupal\entity_test\Entity\EntityTest; class AddressPlainFormatterTest extends FormatterTestBase { /** + * The entity with an address field to test formatting with. + * + * @var \Drupal\Core\Entity\EntityInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->createField('address', 'address_plain'); - } - /** - * Tests the rendered output. - */ - public function testRender() { - $entity = EntityTest::create([]); - $entity->{$this->fieldName} = [ + $this->entity = EntityTest::create([]); + $this->entity->{$this->fieldName} = [ 'country_code' => 'AD', 'locality' => 'Canillo', 'postal_code' => 'AD500', 'address_line1' => 'C. Prat de la Creu, 62-64', ]; - $this->renderEntityFields($entity, $this->display); + + } + + /** + * Tests the rendered output. + */ + public function testRender() { + $this->renderEntityFields($this->entity, $this->display); // Confirm the expected elements, including the predefined locality // (properly escaped), country name. @@ -46,9 +55,40 @@ class AddressPlainFormatterTest extends FormatterTestBase { } // Confirm that an unrecognized locality is shown unmodified. - $entity->{$this->fieldName}->locality = 'FAKE_LOCALITY'; - $this->renderEntityFields($entity, $this->display); + $this->entity->{$this->fieldName}->locality = 'FAKE_LOCALITY'; + $this->renderEntityFields($this->entity, $this->display); $this->assertRaw('FAKE_LOCALITY'); } + /** + * Tests the theme hook suggestions. + * + * @see \Drupal\Tests\node\Functional\NodeTemplateSuggestionsTest + */ + public function testAddressPlainThemeHookSuggestions() { + foreach (['full', 'my_custom_view_mode'] as $view_mode) { + // Simulate themeing of the address test entity. + $variables['theme_hook_original'] = 'address_plain'; + $variables['view_mode'] = $view_mode; + $variables['address'] = $this->entity->{$this->fieldName}; + $suggestions = \Drupal::moduleHandler()->invokeAll('theme_suggestions_address_plain', [$variables]); + + $expected_suggestions = [ + // Hook __ entity_type __ view_mode. + 'address_plain__entity_test__' . $view_mode, + // Hook __ entity_type __ bundle. + 'address_plain__entity_test__entity_test', + // Hook __ entity_type __ bundle __ view_mode. + 'address_plain__entity_test__entity_test__' . $view_mode, + // Hook __ field_name. + 'address_plain__' . $this->fieldName, + // Hook __ entity_type __ field_name. + 'address_plain__entity_test__' . $this->fieldName, + // Hook __ entity_type __ field_name __ bundle. + 'address_plain__entity_test__' . $this->fieldName . '__entity_test', + ]; + $this->assertEqual($suggestions, $expected_suggestions, 'Unexpected theme suggestions for ' . $view_mode); + } + } + }