Skip to content

Instantly share code, notes, and snippets.

@edutrul
Last active April 2, 2017 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edutrul/5d640d5cbaf731e46785c692a9f55622 to your computer and use it in GitHub Desktop.
Save edutrul/5d640d5cbaf731e46785c692a9f55622 to your computer and use it in GitHub Desktop.
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