public function ParagraphsJQueryUIAccordionFormatter::viewElements in Paragraphs jQuery UI Accordion 8
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ ParagraphsJQueryUIAccordionFormatter.php, line 256 - Contains \Drupal\paragraphs_jquery_ui_accordion\Plugin\Field\FieldFormatter\ParagraphsJQueryUIAccordionFormatter.
Class
- ParagraphsJQueryUIAccordionFormatter
- Plugin implementation of the 'paragraphs_jquery_ui_accordion_formatter' formatter.
Namespace
Drupal\paragraphs_jquery_ui_accordion\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$settings = $this
->getSettings();
$elements = [];
$moduleHandler = \Drupal::service('module_handler');
$quickedit = FALSE;
if ($moduleHandler
->moduleExists('quickedit')) {
$quickedit = TRUE;
}
$accordion_id = $this
->getAccordionId($items
->getEntity()
->id());
$js_options = [
'ids' => [
$items
->getEntity()
->id() => $accordion_id,
],
'active' => $this
->getSetting('active') ? 1 : 0,
'autoscroll' => $this
->getSetting('autoscroll'),
'autoscroll_offset' => $this
->getSetting('autoscroll_offset'),
];
if ($settings['autoscroll_offset_toolbar'] && $moduleHandler
->moduleExists('toolbar')) {
if (!\Drupal::currentUser()
->hasPermission('access toolbar')) {
unset($js_options['autoscroll_offset']);
}
}
$elements[0]['accordion'] = [
'#type' => 'container',
'#attributes' => [
'id' => $accordion_id,
],
'#attached' => [
'library' => 'paragraphs_jquery_ui_accordion/accordion',
'drupalSettings' => [
'paragraphs_jquery_ui_accordion' => $js_options,
],
],
];
$title_attributes = [
'class' => [
'accordion-title',
],
];
$content_attributes = [
'class' => [
'accordion-description',
],
];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
// Protect ourselves from recursive rendering.
static $depth = 0;
$depth++;
if ($depth > 20) {
$this->loggerFactory
->get('entity')
->error('Recursive rendering detected when rendering entity @entity_type @entity_id. Aborting rendering.', array(
'@entity_type' => $entity
->getEntityTypeId(),
'@entity_id' => $entity
->id(),
));
return $elements;
}
$title = $entity
->get($this
->getSetting('title'))->value;
$content = $entity
->get($this
->getSetting('content'))
->view($settings['view_mode']);
if (!$settings['simple_id']) {
$id = Html::getUniqueId($this->transliteration
->transliterate($title));
}
else {
$id = $delta + 1;
}
// This variable is needed to avoid js errors during render of paragraph's fields.
if ($quickedit) {
$content_attributes['data-quickedit-entity-id'] = $entity
->getEntityTypeId() . '/' . $entity
->id();
}
$elements[0]['accordion'][$delta] = [
'#theme' => 'paragraphs_jquery_ui_accordion_formatter',
'#title' => $title,
'#content' => $content,
'#id' => $id,
'#title_attributes' => $title_attributes,
'#content_attributes' => $content_attributes,
];
// Add a resource attribute to set the mapping property's value to the
// entity's url. Since we don't know what the markup of the entity will
// be, we shouldn't rely on it for structured data such as RDFa.
if (!empty($items[$delta]->_attributes) && !$entity
->isNew() && $entity
->hasLinkTemplate('canonical')) {
$items[$delta]->_attributes += [
'resource' => $entity
->toUrl()
->toString(),
];
}
$depth = 0;
}
return $elements;
}