Last active
April 2, 2017 21:59
-
-
Save edutrul/5d640d5cbaf731e46785c692a9f55622 to your computer and use it in GitHub Desktop.
How to attach a library from a custom FieldFormatter! :) Works
This file contains 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
<?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