-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Insert module integration for image and file fields #21
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @file | ||
| * Insert module integration for Custom Formatters. | ||
| * | ||
| * Loaded automatically by hook_module_implements_alter() when the | ||
| * insert module is installed. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Drupal\Core\Field\BaseFieldDefinition; | ||
| use Drupal\Core\Field\FormatterInterface; | ||
| use Drupal\custom_formatters\FormatterInterface as CustomFormattersFormatterInterface; | ||
| use Drupal\custom_formatters\InsertFieldItemList; | ||
| use Drupal\file\FileInterface; | ||
|
|
||
| /** | ||
| * Implements hook_insert_styles(). | ||
| */ | ||
| function custom_formatters_insert_styles(string $insert_type): array { | ||
| if (!\Drupal::moduleHandler()->moduleExists('insert')) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!in_array($insert_type, ['image', 'file'], TRUE)) { | ||
| return []; | ||
| } | ||
|
|
||
| $compatible_field_types = $insert_type === 'image' ? ['image'] : ['file', 'entity_reference']; | ||
|
|
||
| $formatters = \Drupal::entityTypeManager() | ||
| ->getStorage('formatter') | ||
| ->loadByProperties(['status' => TRUE]); | ||
|
|
||
| $styles = []; | ||
| foreach ($formatters as $formatter) { | ||
| $formatter_field_types = $formatter->get('field_types') ?? []; | ||
| if (empty(array_intersect($compatible_field_types, $formatter_field_types))) { | ||
| continue; | ||
| } | ||
|
|
||
| $style_name = 'custom_formatters__' . $formatter->id(); | ||
| $styles[$style_name] = [ | ||
| 'label' => $formatter->label(), | ||
| 'weight' => 0, | ||
| ]; | ||
| } | ||
|
|
||
| return $styles; | ||
| } | ||
|
|
||
| /** | ||
| * Implements hook_insert_render(). | ||
| */ | ||
| function custom_formatters_insert_render(string $style_name, array $vars, array $insert_element): string { | ||
| if (!\Drupal::moduleHandler()->moduleExists('insert')) { | ||
| return ''; | ||
| } | ||
|
|
||
| if (!str_starts_with($style_name, 'custom_formatters__')) { | ||
| return ''; | ||
| } | ||
|
|
||
| $formatter_id = substr($style_name, strlen('custom_formatters__')); | ||
|
|
||
| $formatter = \Drupal::entityTypeManager() | ||
| ->getStorage('formatter') | ||
| ->load($formatter_id); | ||
|
|
||
| if (!$formatter instanceof CustomFormattersFormatterInterface || !$formatter->status()) { | ||
| return ''; | ||
| } | ||
|
|
||
| $file = $vars['file'] ?? NULL; | ||
| if (!$file instanceof FileInterface) { | ||
| return ''; | ||
| } | ||
|
|
||
| // Determine the field type based on what the formatter supports. | ||
| $formatter_field_types = $formatter->get('field_types') ?? []; | ||
| $field_type = NULL; | ||
| foreach (['image', 'file', 'entity_reference'] as $type) { | ||
| if (in_array($type, $formatter_field_types, TRUE)) { | ||
| $field_type = $type; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if ($field_type === NULL) { | ||
| return ''; | ||
| } | ||
|
|
||
| // Create a temporary field definition matching the formatter's field type. | ||
| $field_definition = BaseFieldDefinition::create($field_type) | ||
| ->setName('_insert_file') | ||
| ->setTargetEntityTypeId('file'); | ||
|
|
||
| // Suppress alt/title for image fields in this context. | ||
| if ($field_type === 'image') { | ||
| $field_definition->setSetting('alt_field', FALSE); | ||
| $field_definition->setSetting('title_field', FALSE); | ||
| } | ||
|
|
||
| // Create a field item list with the file entity reference. | ||
| $items = new InsertFieldItemList( | ||
| $field_definition, | ||
| '_insert_file', | ||
| $file, | ||
| \Drupal::languageManager()->getCurrentLanguage()->getId(), | ||
| ); | ||
|
|
||
| // Build the field item value based on the resolved field type. | ||
| $value = ['target_id' => (int) $file->id()]; | ||
| if ($field_type === 'image') { | ||
| $value['alt'] = ''; | ||
| $value['title'] = $file->getFilename(); | ||
| } | ||
| elseif ($field_type === 'file') { | ||
| $value['display'] = 1; | ||
| $value['description'] = ''; | ||
| } | ||
| $items->appendItem($value); | ||
|
|
||
| // Instantiate the field formatter plugin and render. | ||
| $ob_level = ob_get_level(); | ||
| try { | ||
| $plugin_manager = \Drupal::service('plugin.manager.field.formatter'); | ||
| $formatter_instance = $plugin_manager->createInstance( | ||
| 'custom_formatters:' . $formatter_id, | ||
| [ | ||
| 'field_definition' => $field_definition, | ||
| 'settings' => [], | ||
| 'label' => 'hidden', | ||
| 'view_mode' => '_custom', | ||
| 'third_party_settings' => [], | ||
| ], | ||
| ); | ||
|
|
||
| \assert($formatter_instance instanceof FormatterInterface); | ||
| $elements = $formatter_instance->viewElements($items, $items->getLangcode()); | ||
| if (empty($elements)) { | ||
| return ''; | ||
| } | ||
|
|
||
| return (string) \Drupal::service('renderer')->renderInIsolation($elements); | ||
| } | ||
| catch (\Throwable $e) { | ||
| // Close any output buffers the formatter engine opened before throwing. | ||
| while (ob_get_level() > $ob_level) { | ||
| ob_end_clean(); | ||
| } | ||
| \Drupal::logger('custom_formatters')->error( | ||
| 'Insert render failed for formatter %id: @message', | ||
| ['%id' => $formatter_id, '@message' => $e->getMessage()], | ||
| ); | ||
| return ''; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Drupal\custom_formatters; | ||
|
|
||
| use Drupal\Core\Field\EntityReferenceFieldItemList; | ||
| use Drupal\Core\TypedData\DataDefinitionInterface; | ||
| use Drupal\file\FileInterface; | ||
|
|
||
| /** | ||
| * Field item list that wraps a file entity for Insert rendering. | ||
| * | ||
| * Extends EntityReferenceFieldItemList (rather than FieldItemList) so that | ||
| * formatters such as ImageFormatter that type-hint against | ||
| * EntityReferenceFieldItemListInterface receive the correct type. | ||
| * | ||
| * In Drupal 11, content entities no longer implement TypedDataInterface, | ||
| * so they cannot be passed as the $parent to FieldItemList. This subclass | ||
| * stores the file entity separately and returns it from getEntity(). | ||
| */ | ||
| class InsertFieldItemList extends EntityReferenceFieldItemList { | ||
|
|
||
| /** | ||
| * The file entity rendered by this field item list. | ||
| * | ||
| * @var \Drupal\file\FileInterface | ||
| */ | ||
| protected FileInterface $entity; | ||
|
|
||
| /** | ||
| * Constructs an InsertFieldItemList. | ||
| * | ||
| * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition | ||
| * The field definition. | ||
| * @param string $name | ||
| * The field name. | ||
| * @param \Drupal\file\FileInterface $file | ||
| * The file entity to render. | ||
| * @param string $langcode | ||
| * The language code. | ||
| */ | ||
| public function __construct(DataDefinitionInterface $definition, string $name, FileInterface $file, string $langcode) { | ||
| parent::__construct($definition, $name, NULL); | ||
| $this->entity = $file; | ||
| $this->langcode = $langcode; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getEntity() { | ||
| return $this->entity; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.