Using the field base hook
If this template with no markup should output field values {{ item.content }}
you need the core code processing these values by defining a base hook.
See for example comment_theme():
/**
* Implements hook_theme().
*/
function comment_theme() {
return [
...
'field__comment' => [
'base hook' => 'field',
],
];
}
In this case it's not enough to start the theme hook name with the base hook, it also needs double underscores: field__raw
.
Implementing a custom theme hook
Not recommended, for demonstration purpose, you can copy the relevant core code to your own template:
mymodule.module:
function mymodule_theme($existing, $type, $theme, $path) {
return [
'field_raw' => [
'render element' => 'element',
],
];
}
function template_preprocess_field_raw(&$variables, $hook) {
$element = $variables['element'];
$variables['items'] = [];
$delta = 0;
while (!empty($element[$delta])) {
$variables['items'][$delta]['content'] = $element[$delta];
$delta++;
}
}
In this case you can choose a random name for the theme hook, but it must not start with field__
.
Switch to this template in the custom field formatter:
public function view(FieldItemListInterface $items, $langcode = NULL) {
$elements = parent::view($items, $langcode);
if (isset($elements['#theme'])) {
$elements['#theme'] = 'field_raw';
}
return $elements;
}
And finally place the minimal template in the module folder
mymodule/templates/field-raw.html.twig
{%- for item in items %}{{ item.content }}{% endfor -%}