1

for example, I have:

  1. module id: module_id (file: module_id.info.yml)
  2. FieldType ID: FieldTypeID (file: /src/plugin/Field/FieldType/FieldType.php)
  3. FieldWidget: FieldDefaultWidget (file: /src/plugin/Field/FieldWidget/FieldDefaultWidget.php)
  4. FieldFormatter: FieldDefaultFormatter (file: /src/plugin/Field/FieldWidget/FieldDefaultFormatter.php)

and inside my field formatter:

public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#theme' => 'my_field',
        '#type' => 'markup',
        // etc...
      ];
    }

    return $elements;
  }
  1. I have file: my_field.module

with function:

function my_field_theme() {
  return array(
    'location_field' => array(
      'variables' => array(
        'x' => NULL,
        'y' => NULL,
        'z' => NULL,
      ),
      'template' => 'my_field',
    ),
  );
}
  1. and I have template file: /templates/my_field.html.twig

as simple as:

<section class="class">
  <h2>name</h2>
  <div class="cls" data-x="x" data-y="y" data-z="z">content</div>
</section>

with that, seems like i can't see my template, which i don't have any idea why?

how is theme or template name relate with the filename (or ID or anything)? - how are they connected each other?

what is the rule for that?

Thank you,

AnD

1

If your code is only in one module with the name module_id, then in 5. the file should be named module_id.module and the function module_id_theme()

  • I have multiple module which will use this field type - will it still applicable? anyway, i will try and see if it works! – AnD Jul 1 '16 at 5:20
  • and in my: my field formatter should i change: '#theme' => 'my_field', into module_id? – AnD Jul 1 '16 at 5:34
  • hmm apparently it doesnt work – AnD Jul 1 '16 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.