--- a/core/lib/Drupal/Core/Field/FormatterBase.php +++ b/core/lib/Drupal/Core/Field/FormatterBase.php @@ -104,23 +104,88 @@ } /** + * Returns the cardinality setting of the field instance. + */ + public function getCardinality() { + if ($this->fieldDefinition instanceof \Drupal\field\Entity\FieldInstanceConfig) { + return $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); + } + return 0; + } + + /** * {@inheritdoc} */ + public static function defaultSettings() { + return array( + 'offset' => 0, + 'limit' => 0, + ) + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ public function settingsForm(array $form, array &$form_state) { + if ($this->getCardinality() == 0) { - return array(); - } + return array(); + } + $element['offset'] = array( + '#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'] = array( + '#type' => 'number', + '#title' => $this->t('Display items'), + '#default_value' => $this->getSetting('limit'), + '#required' => TRUE, + '#min' => 1, + '#description' => $this->t('Number of items to display. Set to 0 to display all items.') + ); + + return $element; + } + /** * {@inheritdoc} */ public function settingsSummary() { - return array(); + $summary = array(); + $limit = $this->getSetting('limit'); + $offset = $this->getSetting('offset'); + + if ($this->getCardinality() == 0 || (empty($limit) && empty($offset))) { + return $summary; - } + } + $summary[] = $this->t('Display %limit items, skip %offset items.', array( + '%limit' => $limit, + '%offset' => $offset + )); + + return $summary; + } + /** * {@inheritdoc} */ - public function prepareView(array $entities_items) { } + public function prepareView(array $entities_items) { + foreach ($entities_items AS $items) { + if ($this->getCardinality() != 0) { + $offset = $this->getSetting('offset'); + $limit = $this->getSetting('limit'); + if (!empty($limit)) { + $items->setValue(array_slice($items->getValue(), $offset, empty($limit) ? NULL : $limit)); + } + } + } + } /** * Returns the array of field settings. =================================================================== --- a/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php @@ -18,6 +18,8 @@ * {@inheritdoc} */ public function prepareView(array $entities_items) { + parent::prepareView($entities_items); + // Remove files specified to not be displayed. $fids = array(); foreach ($entities_items as $items) { =================================================================== --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php @@ -57,14 +57,14 @@ '#options' => $link_types, ); - return $element; + return parent::settingsForm($form, $form_state) + $element; } /** * {@inheritdoc} */ public function settingsSummary() { - $summary = array(); + $summary = parent::settingsSummary(); $image_styles = image_style_options(FALSE); // Unset possible 'No defined styles' option.