Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Provide a formatter which limits and provides offsets #5

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions config/schema/field_formatter.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
1 change: 1 addition & 0 deletions field_formatter.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"limite" is a typo I guess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups you are right.

As always I should have written some tests before :)

On Sun, Jul 10, 2016 at 2:30 PM, marcvangend [email protected]
wrote:

In field_formatter.module
#5 (comment)
:

@@ -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;

"limite" is a typo I guess


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/drupal-media/field_formatter/pull/5/files/ada2ec3a0bfcdc06ba9dbbccba586a64e557bfa2#r70181179,
or mute the thread
https://github.com/notifications/unsubscribe/AABz7oxlvRFsvyPm5fy6D58acyoSPjD3ks5qUOX5gaJpZM4JIyGi
.

}
2 changes: 1 addition & 1 deletion src/Plugin/Field/FieldFormatter/FieldFormatterBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class FieldFormatterBase extends EntityReferenceFormatterBase {
/**
* Entity view display.
*
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface[]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a single display, not array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, then I got confused by varname

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry

On 16 Jul 2016 12:36 p.m., "Andy Postnikov" [email protected]
wrote:

In src/Plugin/Field/FieldFormatter/FieldFormatterBase.php
#5 (comment)
:

@@ -15,7 +15,7 @@
/**
* Entity view display.
*

  • * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
  • * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface[]

Hm, then I got confused by varname


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/drupal-media/field_formatter/pull/5/files/ada2ec3a0bfcdc06ba9dbbccba586a64e557bfa2#r71064413,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABz7nw-qFM3ryUi98acWfMlJoiJzt2Rks5qWLQxgaJpZM4JIyGi
.

*/
protected $viewDisplay;

Expand Down
84 changes: 84 additions & 0 deletions src/Plugin/Field/FieldFormatter/FieldFormatterLimiter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Drupal\field_formatter\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
* Plugin implementation of the 'field_formatter_limiter' formatter.
*
* @FieldFormatter(
* id = "field_formatter_limiter",
* label = @Translation("Limit the amount of deltas being output"),
* field_types = {
* "entity_reference"
* }
* )
*/
class FieldFormatterLimiter extends FieldWrapperBase {

/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings = parent::defaultSettings();
$settings['limit'] = 0;
$settings['offset'] = 0;
return $settings;
}

/**
* Returns the cardinality setting of the field instance.
*/
protected function getCardinality() {
if ($this->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);
}

}
2 changes: 1 addition & 1 deletion src/Plugin/Field/FieldFormatter/FieldWrapperBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above :)

*/
protected $viewDisplay;

Expand Down