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-82: Allow to configure a text format for the video iframe source. #117

Merged
merged 30 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cd640ce
EWPP-82: Update configs for oe_media_iframe module.
sergepavle Jul 15, 2020
e261b58
EWPP-82: Update config schema.
sergepavle Jul 15, 2020
ca71f97
EWPP-82: Update module info file.
sergepavle Jul 15, 2020
85bc81f
EWPP-82: Add upgrade path.
sergepavle Jul 15, 2020
a923ab1
EWPP-82: Add new field widget, update existing media source and field…
sergepavle Jul 15, 2020
c0da85b
EWPP-82: Add functional test and update behat.
sergepavle Jul 15, 2020
440dd9b
EWPP-82: Fix short comment for constructor.
sergepavle Jul 16, 2020
b14ba92
EWPP-82: Fix form element description.
sergepavle Jul 16, 2020
4fca029
EWPP-82: Fix type in the test method.
sergepavle Jul 16, 2020
62a2d98
EWPP-82: Fix text format name.
sergepavle Jul 16, 2020
25cc0cd
EWPP-82: Update dependencies of module.
sergepavle Jul 16, 2020
95c7025
EWPP-82: Update implementation of widget.
sergepavle Jul 16, 2020
c36cf8f
EWPP-82: Update functional test.
sergepavle Jul 16, 2020
97a4cdb
EWPP-82: Update allowed tags (remove redundant global attributes).
sergepavle Jul 17, 2020
a452779
EWPP-82: Fix upgrade path.
sergepavle Jul 17, 2020
3e35263
EWPP-82: Fixes in plugins.
sergepavle Jul 17, 2020
e2bef39
EWPP-82: Update behat tests.
sergepavle Jul 17, 2020
a95ee28
EWPP-82: Fix functional test.
sergepavle Jul 17, 2020
8298a42
EWPP-82: Fix return type.
sergepavle Jul 23, 2020
ace6053
EWPP-82: Update configs for renaming field widget and limit allowed a…
sergepavle Jul 30, 2020
47097e1
EWPP-82: Update post update.
sergepavle Jul 30, 2020
9c586cb
EWPP-82: Update source and field widget.
sergepavle Jul 30, 2020
8d995e7
EWPP-82: Add additional test coverage and update existing.
sergepavle Jul 30, 2020
daf151d
EWPP-82: Change invalidation of config cache.
sergepavle Jul 31, 2020
97a3d8a
EWPP-82: Extend string_textfield schema.
sergepavle Aug 5, 2020
d004f25
EWPP-82: Fix iframe formatter.
sergepavle Aug 5, 2020
b053c60
EWPP-82: Fix iframe widget.
sergepavle Aug 5, 2020
c2e1536
EWPP-82: Fix kernel test for iframe formatter.
sergepavle Aug 5, 2020
60e8a71
EWPP-82: Fix upgrade path.
sergepavle Aug 5, 2020
6cf8348
EWPP-82: Showing filter tips on the iframe video widget.
upchuk Aug 6, 2020
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 @@ -9,6 +9,7 @@ dependencies:
- media.type.video_iframe
module:
- image
- oe_media_iframe
id: media.video_iframe.default
targetEntityType: media
bundle: video_iframe
Expand Down Expand Up @@ -42,7 +43,7 @@ content:
rows: 5
placeholder: ''
third_party_settings: { }
type: string_textarea
type: oe_media_iframe_textarea
region: content
oe_media_iframe_ratio:
weight: 3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
langcode: en
status: true
dependencies: { }
name: 'Iframe media'
format: oe_media_iframe
weight: 0
filters:
filter_html:
id: filter_html
provider: filter
status: true
weight: -10
settings:
allowed_html: '<iframe allowfullscreen height importance loading name referrerpolicy sandbox src width mozallowfullscreen webkitAllowFullScreen scrolling frameborder>'
filter_html_help: true
filter_html_nofollow: false
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ queue_thumbnail_downloads: false
new_revision: false
source_configuration:
source_field: oe_media_iframe
text_format: oe_media_iframe
field_map: { }
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
media.source.oe_media_iframe:
type: media.source.field_aware
label: '"Video iframe" media source configuration'
mapping:
text_format:
type: string
label: 'Text format'

field.widget.settings.oe_media_iframe_textarea:
type: field.widget.settings.string_textarea
label: 'Textarea display format settings'
2 changes: 2 additions & 0 deletions modules/oe_media_iframe/oe_media_iframe.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type: module
core: 8.x

dependencies:
- drupal:filter
- drupal:options
- media:media

Expand All @@ -19,4 +20,5 @@ config_devel:
- field.field.media.video_iframe.oe_media_iframe
- field.field.media.video_iframe.oe_media_iframe_ratio
- field.field.media.video_iframe.oe_media_iframe_thumbnail
- filter.format.oe_iframe_media
- media.type.video_iframe
45 changes: 45 additions & 0 deletions modules/oe_media_iframe/oe_media_iframe.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,48 @@ function oe_media_iframe_post_update_00001(): void {
])->save();
}
}

/**
* Incorporate text format setting into Iframe media source.
*/
function oe_media_iframe_post_update_00002(): void {
$format = \Drupal::entityTypeManager()->getStorage('filter_format')->load('oe_media_iframe');
if (!$format) {
$format = \Drupal::entityTypeManager()->getStorage('filter_format')->create([
'format' => 'oe_media_iframe',
'name' => 'Iframe Media',
'filters' => [
'filter_html' => [
'settings' => [
'allowed_html' => '<iframe allowfullscreen height importance loading name referrerpolicy sandbox src width mozallowfullscreen webkitAllowFullScreen scrolling frameborder>',
],
'status' => TRUE,
],
],
]);
$format->save();
}

$entity_form_storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
$form_display = $entity_form_storage->load('media.video_iframe.default');
$source_field = $form_display->getComponent('oe_media_iframe');
if ($source_field) {
$source_field['type'] = 'oe_media_iframe_textarea';
$form_display->setComponent('oe_media_iframe', $source_field);
$form_display->save();
// We have to remove cache record directly because we can't invalidate
// this cache record by tags ('cache_config' table have empty 'tags' field).
// \Drupal::cache('config')->invalidate() do not work either because core,
// for some reason, does not check the validity of the cache record.
\Drupal::cache('config')->delete('core.entity_form_display.media.video_iframe.default');
}
$media_type_storage = \Drupal::entityTypeManager()->getStorage('media_type');
$iframe_types = $media_type_storage->loadByProperties(['source' => 'oe_media_iframe']);
foreach ($iframe_types as $iframe_media_type) {
$settings = $iframe_media_type->getSource()->getConfiguration();
$settings['text_format'] = 'oe_media_iframe';
$iframe_media_type->set('source_configuration', $settings);
$iframe_media_type->save();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace Drupal\oe_media_iframe\Plugin\Field\FieldFormatter;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\oe_media_iframe\Plugin\media\Source\Iframe;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Plugin implementation for the "oe_media_iframe" formatter plugin.
Expand All @@ -21,7 +25,55 @@
* }
* )
*/
class MediaIframeFormatter extends FormatterBase {
class MediaIframeFormatter extends FormatterBase implements ContainerFactoryPluginInterface {

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

/**
* Constructs a MediaIframeFormatter instance.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
}

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

/**
* {@inheritdoc}
Expand All @@ -48,13 +100,34 @@ public static function isApplicable(FieldDefinitionInterface $field_definition)
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];

$media_type = $this->entityTypeManager->getStorage('media_type')->load($this->fieldDefinition->getTargetBundle());
// Add cacheable dependency from the media type.
$cacheable_metadata = CacheableMetadata::createFromRenderArray($elements)
->addCacheableDependency($media_type);

$text_format = $media_type->getSource()->getConfiguration()['text_format'] ?? NULL;

foreach ($items as $delta => $item) {
if ($text_format) {
$elements[$delta] = [
'#type' => 'processed_text',
'#text' => $item->value,
'#format' => $text_format,
];

$cacheable_metadata->addCacheableDependency($this->entityTypeManager->getStorage('filter_format')->load($text_format));
continue;
}

// Fallback in case we don't have a selected text format.
$elements[$delta] = [
'#markup' => $item->value,
'#allowed_tags' => ['iframe'],
];
}

$cacheable_metadata->applyTo($elements);

return $elements;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_media_iframe\Plugin\Field\FieldWidget;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextareaWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\oe_media_iframe\Plugin\media\Source\Iframe;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Plugin implementation for the "oe_media_iframe" widget plugin.
*
* @FieldWidget(
* id = "oe_media_iframe_textarea",
* label = @Translation("Media iframe text area"),
* description = @Translation("Text area field widget with information about selected text format in the Iframe media source."),
* field_types = {
* "string_long"
* }
* )
*/
class MediaIframeWidget extends StringTextareaWidget {

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

/**
* Constructs a MediaIframeWidget object.
*
* @param string $plugin_id
* The plugin_id for the widget.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the widget is associated.
* @param array $settings
* The widget settings.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
}

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

/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
if ($field_definition->getTargetEntityTypeId() !== 'media') {
return FALSE;
}

$bundle_id = $field_definition->getTargetBundle();
if ($bundle_id === NULL) {
return FALSE;
}

/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($bundle_id);

return $media_type && $media_type->getSource() instanceof Iframe;
}

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);

$media_type = $this->entityTypeManager->getStorage('media_type')->load($this->fieldDefinition->getTargetBundle());
$format_id = $media_type->getSource()->getConfiguration()['text_format'] ?? NULL;

if ($format_id) {
$format = $this->entityTypeManager->getStorage('filter_format')->load($format_id);
$element['format']['guidelines'][$format->id()] = [
'#theme' => 'filter_tips',
'#tips' => _filter_tips($format->id(), FALSE),
];
}

return $element;
}

}
Loading