public function SwitchField::build in Display Suite 8.3
Same name and namespace in other branches
- 8.4 modules/ds_extras/src/Plugin/DsField/SwitchField.php \Drupal\ds_extras\Plugin\DsField\SwitchField::build()
- 8.2 modules/ds_extras/src/Plugin/DsField/SwitchField.php \Drupal\ds_extras\Plugin\DsField\SwitchField::build()
Renders a field.
Return value
array A renderable array representing the content of the field.
Overrides DsFieldBase::build
File
- modules/
ds_extras/ src/ Plugin/ DsField/ SwitchField.php, line 65
Class
- SwitchField
- Plugin that generates a link to switch view mode with via ajax.
Namespace
Drupal\ds_extras\Plugin\DsFieldCode
public function build() {
$settings = $this
->getConfiguration();
if (!empty($settings)) {
/* @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $this
->entity();
// Basic route parameters.
$route_parameters = [
'entityType' => $entity
->getEntityTypeId(),
'entityId' => $entity
->id(),
];
$selector = $this
->viewMode() == 'default' ? 'full' : $this
->viewMode();
// Basic route options.
$route_options = [
'query' => [
'selector' => 'view-mode-' . $selector,
],
'attributes' => [
'class' => [
'use-ajax',
],
],
];
foreach ($settings['vms'] as $key => $value) {
// If the label is empty, do not create a link.
if (!empty($value)) {
$route_parameters['viewMode'] = $key == 'default' ? 'full' : $key;
$url = Url::fromRoute('ds_extras.switch_view_mode', $route_parameters, $route_options);
$items[] = Link::fromTextAndUrl($value, $url)
->toString();
}
}
}
$output = [];
if (!empty($items)) {
$output = [
'#theme' => 'item_list',
'#items' => $items,
// Add the AJAX library to the field for inline switching support.
'#attached' => [
'library' => [
'core/drupal.ajax',
],
],
];
}
return $output;
}