View source
<?php
namespace Drupal\button_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
class ButtonFieldImage extends ButtonFieldBase {
public static function defaultSettings() {
return array(
'image_path' => '',
'alt_text' => '',
'title_text' => '',
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['image_path'] = array(
'#type' => 'textfield',
'#title' => t('Image path'),
'#default_value' => $this
->getSetting('image_path'),
'#required' => TRUE,
);
$element['alt_text'] = array(
'#type' => 'textfield',
'#title' => t('Alt text'),
'#default_value' => $this
->getSetting('alt_text') ?: $this->fieldDefinition
->getLabel(),
'#required' => TRUE,
);
$element['title_text'] = array(
'#type' => 'textfield',
'#title' => t('Title text'),
'#default_value' => $this
->getSetting('title_text'),
'#required' => FALSE,
);
return $element;
}
public function settingsSummary() {
$summary = array();
$summary[] = t('Image path: !path', array(
'!path' => $this
->getSetting('image_path'),
));
$summary[] = t('Alt text: !text', array(
'!text' => $this
->getSetting('alt_text') ?: $this->fieldDefinition
->getLabel(),
));
$summary[] = t('Title text: !text', array(
'!text' => $this
->getSetting('title_text'),
));
return $summary;
}
protected function elementProperties() {
return array(
'#type' => 'image_button',
'#src' => $this
->getSetting('image_path'),
'#attributes' => array(
'alt' => $this
->getSetting('alt_text') ?: $this->fieldDefinition
->getLabel(),
'title' => $this
->getSetting('title_text'),
),
);
}
}