Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-287: Create organisation teaser #595

Merged
merged 3 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class EventExtraFieldBase extends ExtraFieldDisplayFormattedBase implem
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityViewBuilderInterface
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ content:
type: entity_reference_revisions_entity_view
region: content
hidden:
extra_field_oe_theme_content_organisation_teaser_details: true
extra_field_oe_theme_helper_short_title_with_fallback: true
langcode: true
oe_content_content_owner: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
langcode: en
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2020-09-24 at 19 40 02

current teaser view

status: true
dependencies:
config:
- core.entity_view_mode.node.teaser
- field.field.node.oe_organisation.body
- field.field.node.oe_organisation.oe_organisation_acronym
- field.field.node.oe_organisation.oe_organisation_contact
- field.field.node.oe_organisation.oe_organisation_logo
- field.field.node.oe_organisation.oe_summary
- field.field.node.oe_organisation.oe_teaser
- image.style.oe_theme_ratio_3_2_medium
- node.type.oe_organisation
module:
- oe_theme_helper
- text
- user
id: node.oe_organisation.teaser
targetEntityType: node
bundle: oe_organisation
mode: teaser
content:
extra_field_oe_theme_content_organisation_teaser_details:
weight: 2
region: content
settings: { }
third_party_settings: { }
oe_organisation_acronym:
type: string
weight: 0
region: content
label: above
settings:
link_to_entity: false
third_party_settings: { }
oe_organisation_logo:
type: oe_theme_helper_media_thumbnail_url
weight: 3
region: content
label: above
settings:
image_style: oe_theme_ratio_3_2_medium
third_party_settings: { }
oe_teaser:
type: text_default
weight: 1
region: content
label: above
settings: { }
third_party_settings: { }
hidden:
body: true
extra_field_oe_theme_helper_short_title_with_fallback: true
langcode: true
links: true
oe_content_content_owner: true
oe_content_legacy_link: true
oe_content_navigation_title: true
oe_content_short_title: true
oe_organisation_contact: true
oe_summary: true
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function oe_theme_content_organisation_install() {
$storage = new FileStorage(drupal_get_path('module', 'oe_theme_content_organisation') . '/config/overrides');
$displays = [
'core.entity_view_display.node.oe_organisation.default',
'core.entity_view_display.node.oe_organisation.teaser',
];

foreach ($displays as $display) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_theme_content_organisation\Plugin\ExtraField\Display;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\extra_field\Plugin\ExtraFieldDisplayFormattedBase;
use Drupal\oe_content_entity_contact\Entity\ContactInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Display contact information.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Display contact information.
* Display organisation details.

*
* @ExtraFieldDisplay(
* id = "oe_theme_content_organisation_teaser_details",
* label = @Translation("Teaser details"),
* bundles = {
* "node.oe_organisation",
* },
* visible = true
* )
*/
class TeaserDetailsExtraField extends ExtraFieldDisplayFormattedBase implements ContainerFactoryPluginInterface {

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* TeaserDetailsExtraField constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}

/**
* {@inheritdoc}
*/
public function viewElements(ContentEntityInterface $entity) {
// Return an empty array if empty, so the field can be considered empty.
if ($entity->get('oe_organisation_contact')->isEmpty()) {
return [];
}

$contact = $entity->get('oe_organisation_contact')->entity;
if (!$contact instanceof ContactInterface) {
return [];
}

$build = [
'#type' => 'pattern',
'#id' => 'field_list',
'#variant' => 'horizontal',
'#fields' => [
'items' => [],
],
];
$cache = CacheableMetadata::createFromRenderArray($build);

$contact_access = $contact->access('view', NULL, TRUE);
$cache->addCacheableDependency($contact_access);

if (!$contact_access->isAllowed()) {
$cache->applyTo($build);
return $build;
}

$cache->addCacheableDependency($contact);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's maybe move this before line 89? Changes on the entity might have a consequence on its access.

$items = [];
$fields = [
'oe_website' => [],
'oe_email' => ['type' => 'email_mailto'],
'oe_phone' => [],
'oe_address' => [
'type' => 'oe_theme_helper_address_inline',
'settings' => ['delimiter' => ', '],
],
];
foreach ($fields as $field_name => $display_options) {
$item = $this->getRenderableFieldListItem($contact, $field_name, $display_options);
if (!empty($item)) {
$items[] = $item;
}
ademarco marked this conversation as resolved.
Show resolved Hide resolved
}
$build['#fields']['items'] = $items;
$cache->applyTo($build);

return $build;
}

/**
* Gets renderable item for field list pattern.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Gets renderable item for field list pattern.
* Get renderable item for field list pattern.

*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* Content entity.
* @param string $field_name
* Field name.
* @param array $display_options
* Display options for field rendering.
*
* @return array
* Renderable array.
*/
protected function getRenderableFieldListItem(ContentEntityInterface $entity, string $field_name, array $display_options = []): array {
if ($entity->get($field_name)->isEmpty()) {
return [];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this if before 109, so we only check for the empty field and we don't have to check for if (!empty($item)) {. It will be the responsibility of the called to call this function knowing that it will return.


$display_options += [
'label' => 'hidden',
];
$renderable = $this->entityTypeManager->getViewBuilder('oe_contact')
->viewField($entity->get($field_name), $display_options);

return [
'label' => $renderable['#title'],
'body' => $renderable,
];
}

}
18 changes: 18 additions & 0 deletions templates/content/node--oe-organisation--teaser.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{#
/**
* @file
* Theme override to display a node of bundle organisation in the teaser view mode.
*/
#}
{% set image_url = content.oe_organisation_logo|field_value|render %}
{{ pattern('list_item', {
'variant': 'thumbnail_secondary',
'url': url,
'meta': content.oe_organisation_acronym|field_value,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meta should accept an array, change it into:

 'meta': [
  content.oe_organisation_acronym|field_value,
]

'title': label,
'detail': content.oe_teaser|field_value,
'additional_information': [
content.extra_field_oe_theme_content_organisation_teaser_details,
],
'image': image_url ? { 'src': image_url }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not pass the alt value of the image here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not doing that anywhere actually, as the image is displayed as a background anyway and we want to refactor that into using media container. I'd postpone this to this follow up: EWPP-249 which has already some POC work done here #569

}) }}
Loading