Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 299521

LanguageFormatter is not used for nodes because it's still hard coded in NodeViewBuilder

$
0
0

Problem/Motivation

NodeViewBuilder hardcodes the display of the langcode field ignoring any formatter configuration. This is a barrier if you want to create new formatter such as:

  • Display name with native names
  • Add flag icons for languages
  • Display source language

Proposed resolution

Remove the hardcode langcode element in NodeViewBuilder and keep the wrapper for BC.

Remaining tasks

Review

User interface changes

The display of the language field will change a little, because the 'field-language-display' wrapper will be gone.

API changes

The 'field-language-display' wrapper will be gone. However, this fixes a potential issue with having multiple same ID's on the same page, e.g. on a node list.

Data model changes

None

-- Original report --

Any options that are selected for language field formatter are ignored and the language field is always displayed as plain text language name.

Steps to reproduce:

  1. Enable translations.
  2. Create translatable content type.
  3. Select language field to be displayed.
  4. Check formatter option "Link to the Content".
  5. Create new node.
  6. View the node and you'll see plain text language name instead of a link.

I have noticed this when I was writing custom field formatter for the language field. It's easy to see the problem once you try to change the output for the core's language field. For example try this formatter:

namespace Drupal\custom_formatter\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\LanguageFormatter as BaseFormatter;

/**
 * Plugin implementation of the 'custom_formatter' formatter.
 *
 * @FieldFormatter(
 *   id = "language_custom_fomatter",
 *   label = @Translation("Language with flag"),
 *   field_types = {
 *     "language"
 *   }
 * )
 */
class LanguageCustomFormatter extends BaseFormatter {

  /**
   * {@inheritdoc}
   */
  protected function viewValue(FieldItemInterface $item) {
    $view = parent::viewValue($item);

    $view['#plain_text'] .= ' Test';

    return $view;
  }
}

The formatter itself is registered correctly, you can enable it for the language field. The settings form works fine, but the actual output of the formatter is totally ignored. For example instead of seeing "English Test" I always get "English".


Viewing all articles
Browse latest Browse all 299521

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>