Commit 2b75d833 authored by dww's avatar dww

Issue #2856797 by dww, validoll, hstandaert, neetu morwani, New Zeal, kaythay,...

Issue #2856797 by dww, validoll, hstandaert, neetu morwani, New Zeal, kaythay, bojanz, mpp: Missing theme suggestions for address_plain
parent 502ee52c
......@@ -24,11 +24,34 @@ function address_theme() {
'locality' => [],
'administrative_area' => [],
'country' => [],
'address' => NULL,
'view_mode' => '',
],
],
];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function address_theme_suggestions_address_plain(array $variables) {
$suggestions = [];
$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;
}
/**
* Updates the given entity's field for the RC1 changes.
*
......
......@@ -147,6 +147,8 @@ class AddressPlainFormatter extends FormatterBase implements ContainerFactoryPlu
'code' => $country_code,
'name' => $countries[$country_code],
],
'#address' => $address,
'#view_mode' => $this->viewMode,
'#cache' => [
'contexts' => [
'languages:' . LanguageInterface::TYPE_INTERFACE,
......
......@@ -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,
......
......@@ -11,6 +11,13 @@ 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}
*/
......@@ -18,20 +25,22 @@ class AddressPlainFormatterTest extends FormatterTestBase {
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->assertEquals($expected_suggestions, $suggestions, 'Unexpected theme suggestions for ' . $view_mode);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment