Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / field / formatter.twig
diff --git a/vendor/chi-teck/drupal-code-generator/templates/d8/plugin/field/formatter.twig b/vendor/chi-teck/drupal-code-generator/templates/d8/plugin/field/formatter.twig
new file mode 100644 (file)
index 0000000..a2b0ad1
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+namespace Drupal\{{ machine_name }}\Plugin\Field\FieldFormatter;
+
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Field\FormatterBase;
+use Drupal\Core\Form\FormStateInterface;
+
+/**
+ * Plugin implementation of the '{{ plugin_label }}' formatter.
+ *
+ * @FieldFormatter(
+ *   id = "{{ plugin_id }}",
+ *   label = @Translation("{{ plugin_label }}"),
+ *   field_types = {
+ *     "string"
+ *   }
+ * )
+ */
+class {{ class }} extends FormatterBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function defaultSettings() {
+    return [
+      'prefix' => '',
+      'suffix' => '',
+    ] + parent::defaultSettings();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function settingsForm(array $form, FormStateInterface $form_state) {
+    $settings = $this->getSettings();
+
+    $elements['prefix'] = [
+      '#type' => 'textfield',
+      '#title' => $this->t('Prefix'),
+      '#default_value' => $settings['prefix'],
+    ];
+
+    $elements['suffix'] = [
+      '#type' => 'textfield',
+      '#title' => $this->t('Suffix'),
+      '#default_value' => $settings['suffix'],
+    ];
+
+    return $elements;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function settingsSummary() {
+    $settings = $this->getSettings();
+    $summary = [];
+
+    if ($settings['prefix']) {
+      $summary[] = $this->t('Prefix: @prefix', ['@prefix' => $settings['prefix']]);
+    }
+    if ($settings['suffix']) {
+      $summary[] = $this->t('Suffix: @suffix', ['@suffix' => $settings['suffix']]);
+    }
+
+    return $summary;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function viewElements(FieldItemListInterface $items, $langcode) {
+    $element = [];
+    $settings = $this->getSettings();
+
+    foreach ($items as $delta => $item) {
+      $element[$delta] = [
+        '#type' => 'item',
+        '#markup' => $item->value,
+        '#field_prefix' => $settings['prefix'],
+        '#field_suffix' => $settings['suffix'],
+      ];
+    }
+
+    return $element;
+  }
+
+}