public function ButtonFieldBase::viewElements in Button Field 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/ ButtonFieldBase.php, line 19 - Contains Drupal\button_field\Plugin\Field\FieldFormatter\ButtonFieldBase.
Class
- ButtonFieldBase
- Base implementation for button field formatters.
Namespace
Drupal\button_field\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
// Get the entity that we're rendering the field for.
$entity = $items
->getEntity();
$entity_type = $entity
->getEntityType()
->id();
$entity_id = $entity
->id();
// If any additional classes have been defined then add them to the classes
// now.
$class = array(
'button_field',
);
$additional_classes = $this
->getFieldSetting('additional_classes');
if (!empty($additional_classes)) {
$class = array_merge($class, explode(' ', $additional_classes));
}
// Button elements do not obey the #description index, so if a description
// has been set we need to build our own suffix.
$suffix = NULL;
$description = $this->fieldDefinition
->getDescription();
if (!empty($description)) {
$suffix = '<div class="description">' . $description . '</div>';
}
$id = $this
->elementId(0, $items
->getLangcode());
$element = array(
'#id' => $id,
'#name' => $id,
'#attributes' => array(
'class' => $class,
),
'#description' => t('this is the help text'),
'#suffix' => $suffix,
'#entity_type' => $entity_type,
'#entity' => $entity,
'#field_name' => $items
->getName(),
'#attached' => array(
'drupalSettings' => array(
'button_field' => array(
$id => array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'confirmation' => '',
),
),
),
),
'#ajax' => array(
'callback' => 'button_field_callback_ajax',
),
);
$element += $this
->elementProperties();
return \Drupal::formBuilder()
->getForm('Drupal\\button_field\\Form\\DummyForm', $element);
}