0

I have a field of list type in my custom content type. The display format of list type field has two options default(value) and key. In my case i want third option which will display "key and value". How can i add third option or get the key/value for the field.

2

Here is an example on how to create custom field formatters

https://github.com/npinos/drupal8-field-formatters

You can directly specify the markup like this extending the FormatterBase class:

<?php
/**
 * @file
 * Contains Drupal\drupal8_field_formatters\Plugin\Field\FieldFormatter\MainHeadingFormatter.
 */
namespace Drupal\drupal8_field_formatters\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
/**
 * Plugin implementation of the 'main_heading_formatter' formatter.
 *
 * @FieldFormatter(
 *   id = "main_heading_formatter",
 *   label = @Translation("Heading 1"),
 *   weight = "10",
 *   field_types = {
 *     "string",
 *     "text",
 *   },
 *   quickedit = {
 *     "editor" = "plain_text"
 *   }
 * )
 */
class MainHeadingFormatter extends FormatterBase {
  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $element = [];
    foreach ($items as $delta => $item) {
      // Render each element as markup.
      $element[$delta] = [
        '#type' => 'markup',
        '#markup' => '<h1>' . $item->value . '</h1>',
      ];
    }
    return $element;
  }
}
1

You will require to write your own custom field formatter to achieve that. There are detailed docs at : https://www.drupal.org/docs/8/creating-custom-modules/create-a-custom-field-formatter

File /modules/custom_module/src/Plugin/Field/FieldFormatter/KeyValueFormatter.php

<?php

namespace Drupal\custom_module\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'key_value' formatter.
 *
 * @FieldFormatter(
 *   id = "key_value_list",
 *   label = @Translation("Key - value"),
 *   field_types = {
 *     "list_string"
 *   }
 * )
 */

The field_types key determines the type of the field this formatter will be available to. Just override the viewElements function in the KeyValueFormatter class and let them display key - value.

I would recommend extending Drupal\options\Plugin\Field\FieldFormatter\OptionsDefaultFormatter instead of FormatterBase as suggested by the docs.

  • Drupal\options\Plugin\Field\FieldFormatter\OptionsDefaultFormatter also extending FormatterBase . Good thing will be i will have secure code and won't have to include 'use Drupal\Core\Field\AllowedTagsXssTrait;' and other namespaces. You do make sense on the above. Docs are for generic formatter. – Aditya Joshi Jan 24 '18 at 21:59
  • @AdityaJoshi On the contrary, the AllowedTagsXssTrait helps you prevent the XSS vulnerabilities by filtering any HTML that might be a part of the field settings. – AjitS Jan 25 '18 at 4:05

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.