From ada2ec3a0bfcdc06ba9dbbccba586a64e557bfa2 Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Sun, 10 Jul 2016 09:26:46 +0200 Subject: [PATCH] Provide a formatter which limits and provides offsets --- config/schema/field_formatter.schema.yml | 16 ++++ field_formatter.module | 1 + .../FieldFormatter/FieldFormatterBase.php | 2 +- .../FieldFormatter/FieldFormatterLimiter.php | 84 +++++++++++++++++++ .../Field/FieldFormatter/FieldWrapperBase.php | 2 +- 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/Plugin/Field/FieldFormatter/FieldFormatterLimiter.php diff --git a/config/schema/field_formatter.schema.yml b/config/schema/field_formatter.schema.yml index 1b8aee1..65d8f80 100644 --- a/config/schema/field_formatter.schema.yml +++ b/config/schema/field_formatter.schema.yml @@ -30,3 +30,19 @@ field.formatter.settings.field_link: settings: label: 'Formatter settings' type: field.formatter.settings.[%parent.type] + +field.formatter.settings.field_formatter_limiter: + type: mapping + mapping: + type: + label: 'Machine name of the formatter' + type: string + settings: + label: 'Formatter settings' + type: field.formatter.settings.[%parent.type] + offset: + type: integer + label: 'Offset' + limit: + type: integer + label: 'Limit' diff --git a/field_formatter.module b/field_formatter.module index 71e916a..65724bb 100644 --- a/field_formatter.module +++ b/field_formatter.module @@ -14,4 +14,5 @@ function field_formatter_field_formatter_info_alter(array &$info) { $field_types = array_keys($field_type_manager->getDefinitions()); $info['field_link']['field_types'] = $field_types; + $info['field_formatter_limite']['field_types'] = $field_types; } diff --git a/src/Plugin/Field/FieldFormatter/FieldFormatterBase.php b/src/Plugin/Field/FieldFormatter/FieldFormatterBase.php index a45e1f3..3dae6d2 100644 --- a/src/Plugin/Field/FieldFormatter/FieldFormatterBase.php +++ b/src/Plugin/Field/FieldFormatter/FieldFormatterBase.php @@ -15,7 +15,7 @@ abstract class FieldFormatterBase extends EntityReferenceFormatterBase { /** * Entity view display. * - * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface + * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface[] */ protected $viewDisplay; diff --git a/src/Plugin/Field/FieldFormatter/FieldFormatterLimiter.php b/src/Plugin/Field/FieldFormatter/FieldFormatterLimiter.php new file mode 100644 index 0000000..8099687 --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/FieldFormatterLimiter.php @@ -0,0 +1,84 @@ +fieldDefinition instanceof FieldDefinitionInterface) { + return $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); + } + return 0; + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + if ($this->getCardinality() == 1) { + return []; + } + + $element['offset'] = [ + '#type' => 'number', + '#title' => $this->t('Skip items'), + '#default_value' => $this->getSetting('offset'), + '#required' => TRUE, + '#min' => 0, + '#description' => $this->t('Number of items to skip from the beginning.') + ]; + + $element['limit'] = [ + '#type' => 'number', + '#title' => $this->t('Display items'), + '#default_value' => $this->getSetting('limit'), + '#required' => TRUE, + '#min' => 0, + '#description' => $this->t('Number of items to display. Set to 0 to display all items.') + ]; + + return $element; + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + $field_output = $this->getFieldOutput($items, $langcode); + + $offset = $this->getSetting('offset'); + // Array_slice needs NULL to show all elements. + $limit = $this->getSetting('limit') == 0 ? NULL : $this->getSetting('limit'); + + return array_slice($field_output, $offset, $limit); + } + +} diff --git a/src/Plugin/Field/FieldFormatter/FieldWrapperBase.php b/src/Plugin/Field/FieldFormatter/FieldWrapperBase.php index ed375d4..07ee8ab 100644 --- a/src/Plugin/Field/FieldFormatter/FieldWrapperBase.php +++ b/src/Plugin/Field/FieldFormatter/FieldWrapperBase.php @@ -22,7 +22,7 @@ abstract class FieldWrapperBase extends FormatterBase implements ContainerFactor /** * Entity view display. * - * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface + * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface[] */ protected $viewDisplay;