View source
<?php
namespace Drupal\remote_image\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\link\Plugin\Field\FieldWidget\LinkWidget;
class RemoteImageWidget extends LinkWidget {
public static function defaultSettings() {
return [
'placeholder_url' => '',
'placeholder_title' => '',
'placeholder_alt' => '',
'placeholder_width' => '',
'placeholder_height' => '',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$field_settings = $this->fieldDefinition
->getSettings();
$elements = parent::settingsForm($form, $form_state) + [
'placeholder_alt' => !$field_settings['alt_field'] ? [] : [
'#type' => 'textfield',
'#title' => $this
->t('Placeholder for alt text'),
'#default_value' => $this
->getSetting('placeholder_alt'),
'#description' => $this
->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
],
];
$elements['placeholder_title'] = !$field_settings['title_field'] ? [] : [
'#title' => $this
->t('Placeholder for image title'),
'#states' => [],
] + $elements['placeholder_title'];
return $elements;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['uri']['#weight'] = 5;
$item = $items[$delta];
return [
'alt' => [
'#type' => 'textfield',
'#title' => $this
->t('Alternative text'),
'#description' => $this
->t('This text will be used by screen readers, search engines, and when the image cannot be loaded.'),
'#default_value' => $item->alt,
'#placeholder' => $this
->getSetting('placeholder_alt'),
'#weight' => 10,
'#maxlength' => 512,
],
'title' => [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#description' => t('The title attribute is used as a tooltip when the mouse hovers over the image.'),
'#default_value' => $item->title,
'#placeholder' => $this
->getSetting('placeholder_title'),
'#weight' => 15,
'#maxlength' => 1024,
],
'width' => [
'#type' => 'number',
'#title' => $this
->t('Width'),
'#description' => t('The width of the image'),
'#weight' => 20,
'#default_value' => $item->width ?: 0,
],
'height' => [
'#type' => 'number',
'#title' => $this
->t('Height'),
'#description' => t('The height of the image.'),
'#weight' => 25,
'#default_value' => $item->height ?: 0,
],
] + $element;
}
}