I wish to register a template file for use with a filed item, but I can't wrap my head around it.
here is what I am trying to achieve. In my field widget Formatter
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => $this->viewValue($item),
'#theme' => 'my-field-item-template',
];
}
return $elements;
}
Then in my .module file
function mymodule_theme($existing, $type, $theme, $path){
return array('myplugin' =>
array(
'variables' => array(
'other elements' => 'other elements',
),
'template' => 'my-field-item-template'
)
);
}
I wish to print my 'variables' in my twig file, I believe my template file is not registered for use with my field item this way. What's the right way?