You are here

Count.php in Field Count Formatter 2.x

Same filename and directory in other branches
  1. 8 src/Plugin/Field/FieldFormatter/Count.php

File

src/Plugin/Field/FieldFormatter/Count.php
View source
<?php

/**
 * @file
 * Contains \Drupal\field_count_formatter\Plugin\Field\FieldFormatter\Count.
 */
namespace Drupal\field_count_formatter\Plugin\Field\FieldFormatter;

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

/**
 * Plugin implementation of the 'CountFormatter' formatter.
 *
 * @FieldFormatter(
 *   id = "count",
 *   label = @Translation("Field count"),
 *   field_types = {
 *   }
 * )
 */
class Count extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    return [
      $this
        ->t('Displays the number of items/count.'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {

    // Needs to be nested (element 0) so we preserve the default field title rendering.
    return [
      [
        '#markup' => $items
          ->count(),
      ],
    ];
  }

}

Classes

Namesort descending Description
Count Plugin implementation of the 'CountFormatter' formatter.