Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Merge pull request #22 from openeuropa/OPENEUROPA-1775
Browse files Browse the repository at this point in the history
OPENEUROPA-1775: Add responsive image formatter.
  • Loading branch information
hernani authored May 23, 2019
2 parents ecc3815 + 55dd26e commit 5a909b8
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 24 deletions.
1 change: 1 addition & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
selenium:
image: selenium/standalone-chrome:3
shm_size: 2g
environment:
- DISPLAY=:99
- SE_OPTS=-debug
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"drupal-composer/drupal-scaffold": "~2.5.2",
"cweagans/composer-patches": "^1.6",
"drupal/config_devel": "~1.2",
"drupal/console": "^1.6",
"drupal/drupal-extension": "~4.0",
"drush/drush": "~9.0",
"nikic/php-parser": "^3.1.5",
Expand Down
9 changes: 9 additions & 0 deletions config/schema/media_avportal.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,14 @@ field.formatter.settings.avportal_photo:
type: string
label: 'Image style'

field.formatter.settings.avportal_photo_responsive:
type: mapping
label: 'Responsive image format settings'
mapping:
responsive_image_style:
type: string
label: 'Responsive image style'


field.widget.settings.avportal_textfield:
type: field.widget.settings.string_textfield
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ services:
- '5900:5900'
volumes:
- /dev/shm:/dev/shm
shm_size: 2g


#### Mac users: uncomment the "volumes" key to enable the NFS file sharing. You can find more information about Docker for Mac here: https://github.com/openeuropa/openeuropa/blob/master/docs/starting/tooling.md#using-docker-on-macos
Expand Down
3 changes: 2 additions & 1 deletion runner.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ commands:
- { task: "run", command: "setup:phpunit" }
setup:phpunit:
- { task: "process", source: "phpunit.xml.dist", destination: "phpunit.xml" }

# Generate settings.testing.php, it will be used when running functional tests.
- { task: "process-php", type: "write", config: "drupal.settings", source: "${drupal.root}/sites/default/default.settings.php", destination: "${drupal.root}/sites/default/settings.testing.php", override: true }
39 changes: 17 additions & 22 deletions src/Plugin/Field/FieldFormatter/AvPortalPhotoFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\image\ImageStyleInterface;
use Drupal\media\Entity\MediaType;
Expand Down Expand Up @@ -80,26 +79,23 @@ class AvPortalPhotoFormatter extends FormatterBase implements ContainerFactoryPl
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory service.
* @param \Drupal\media_avportal\AvPortalClientInterface $avPortalClient
* @param \Drupal\media_avportal\AvPortalClientInterface $avportal_client
* The AV Portal client.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, MessengerInterface $messenger, LoggerChannelFactoryInterface $logger_factory, AvPortalClientInterface $avPortalClient, ConfigFactoryInterface $configFactory, EntityTypeManagerInterface $entityTypeManager) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, LoggerChannelFactoryInterface $logger_factory, AvPortalClientInterface $avportal_client, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->messenger = $messenger;
$this->logger = $logger_factory->get('media');
$this->avPortalClient = $avPortalClient;
$this->config = $configFactory->get('media_avportal.settings');
$this->entityTypeManager = $entityTypeManager;
$this->avPortalClient = $avportal_client;
$this->config = $config_factory->get('media_avportal.settings');
$this->entityTypeManager = $entity_type_manager;
}

/**
Expand All @@ -114,7 +110,6 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('messenger'),
$container->get('logger.factory'),
$container->get('media_avportal.client'),
$container->get('config.factory'),
Expand Down Expand Up @@ -199,16 +194,21 @@ public static function isApplicable(FieldDefinitionInterface $field_definition):
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode): array {
$cache = new CacheableMetadata();
$list_cache_tags = $this->entityTypeManager->getDefinition('image_style')->getListCacheTags();
$cache->addCacheTags($list_cache_tags);

$elements = [];

foreach ($items as $delta => $item) {
$elements[$delta] = $this->viewElement($item);
}

$cache = new CacheableMetadata();
$image_style = $this->settings['image_style'] ?? NULL;
if (!empty($image_style)) {
$image_style = $this->entityTypeManager->getStorage('image_style')->load($image_style);
if ($image_style instanceof ImageStyleInterface) {
$cache->addCacheableDependency($image_style);
}
}

$cache->applyTo($elements);

return $elements;
Expand Down Expand Up @@ -241,11 +241,9 @@ protected function viewElement(FieldItemInterface $item): array {
return [];
}

$cache = new CacheableMetadata();

$image_style = $this->settings['image_style'] ?? NULL;
$theme = 'image';
if ($image_style && $image_style !== '') {
if (!empty($image_style)) {
$image_style = $this->entityTypeManager->getStorage('image_style')->load($image_style);
if ($image_style instanceof ImageStyleInterface) {
$theme = 'image_style';
Expand All @@ -262,11 +260,8 @@ protected function viewElement(FieldItemInterface $item): array {

if ($theme === 'image_style') {
$build['#style_name'] = $image_style->id();
$cache->addCacheableDependency($image_style);
}

$cache->applyTo($build);

return $build;
}

Expand Down
Loading

0 comments on commit 5a909b8

Please sign in to comment.