-
Notifications
You must be signed in to change notification settings - Fork 2
Provide a formatter which limits and provides offsets #5
base: 8.x-1.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ abstract class FieldFormatterBase extends EntityReferenceFormatterBase { | |
/** | ||
* Entity view display. | ||
* | ||
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface | ||
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a single display, not array There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, then I got confused by varname There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]
|
||
*/ | ||
protected $viewDisplay; | ||
|
||
|
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); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not array There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above :) |
||
*/ | ||
protected $viewDisplay; | ||
|
||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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: