Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to attach a library from a custom FieldFormatter! :) Works
<?php
namespace Drupal\custom\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
/**
* Plugin implementation of the 'Custom' formatter.
*
* @FieldFormatter(
* id = "custom_formatter",
* label = @Translation("Custom Formatter"),
* field_types = {
* "string"
* }
* )
*/
class CustomFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
foreach ($items as $delta => $item) {
// Render each element as markup.
$element[$delta] = [
'#theme' => 'mycustom_theme',
'#title' => 'hey hey',
'#attached' => ['library' => ['custom/custom.mylibrary']]
];
}
return $element;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.