View source
<?php
namespace Drupal\webform_image_select\Plugin\WebformElement;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElement\Select;
use Drupal\webform_image_select\Element\WebformImageSelect as WebformImageSelectElement;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_image_select\Entity\WebformImageSelectImages;
class WebformImageSelect extends Select {
protected function defineDefaultProperties() {
$properties = [
'images' => [],
'images_randomize' => FALSE,
'show_label' => FALSE,
'filter' => FALSE,
'filter__placeholder' => (string) $this
->t('Filter images by label'),
'filter__singlular' => (string) $this
->t('image'),
'filter__plural' => (string) $this
->t('images'),
'filter__no_results' => (string) $this
->t('No images found.'),
] + parent::defineDefaultProperties();
unset($properties['options'], $properties['options_randomize'], $properties['field_prefix'], $properties['field_suffix'], $properties['disabled'], $properties['select2']);
return $properties;
}
protected function defineTranslatableProperties() {
return array_merge(parent::defineTranslatableProperties(), [
'images',
'filter__placeholder',
'filter__singlular',
'filter__plural',
'filter__no_results',
]);
}
public function initialize(array &$element) {
$element['#images'] = WebformImageSelectImages::getElementImages($element);
WebformImageSelectElement::setOptions($element);
parent::initialize($element);
}
public function getRelatedTypes(array $element) {
return [];
}
protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
$format = $this
->getItemFormat($element);
if ($format === 'image') {
if (isset($element['#images'][$value]) && isset($element['#images'][$value]['src'])) {
$src = $element['#images'][$value]['src'];
if (strpos($src, '/') === 0) {
$src = \Drupal::request()
->getSchemeAndHttpHost() . $src;
}
$image = [
'#theme' => 'image',
'#attributes' => [
'src' => $src,
],
'#title' => $element['#images'][$value]['text'],
'#alt' => $element['#images'][$value]['text'],
];
if ($image_size = @getimagesize($element['#images'][$value]['src'])) {
$image['#width'] = $image_size[0];
$image['#height'] = $image_size[1];
}
if (strpos(\Drupal::routeMatch()
->getRouteName(), 'webform.results_submissions') !== FALSE) {
$image['#attached']['library'][] = 'webform/webform.tooltip';
$image['#attributes']['class'] = [
'js-webform-tooltip-link',
];
return $image;
}
$build = [
'#prefix' => new FormattableMarkup('<figure style="display: inline-block; margin: 0 6px 6px 0; padding: 6px; border: 1px solid #ddd;' . (isset($image['#width']) ? 'width: ' . $image['#width'] . 'px' : '') . '">', []),
'#suffix' => '</figure>',
'image' => $image,
];
if (!empty($element['#show_label'])) {
$build['caption'] = [
'#markup' => $element['#images'][$value]['text'],
'#prefix' => '<figcaption>',
'#suffix' => '</figcaption>',
];
}
return $build;
}
else {
return $value;
}
}
else {
return parent::formatHtmlItem($element, $webform_submission, $options);
}
}
protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
if ($this
->getItemFormat($element) === 'image') {
$element['#format'] = 'value';
}
return parent::formatTextItem($element, $webform_submission, $options);
}
public function getItemFormats() {
return parent::getItemFormats() + [
'image' => $this
->t('Image'),
];
}
public function getItemDefaultFormat() {
return 'image';
}
public function getItemsDefaultFormat() {
return 'space';
}
public function getItemsFormats() {
return parent::getItemsFormats() + [
'br' => $this
->t('Break'),
'space' => $this
->t('Space'),
];
}
public function preview() {
return parent::preview() + [
'#show_label' => TRUE,
'#images' => [
'dog_1' => [
'text' => 'Dog 1',
'src' => 'https://www.placedog.net/80/100',
],
'dog_2' => [
'text' => 'Dog 2',
'src' => 'https://www.placedog.net/100/100',
],
'dog_3' => [
'text' => 'Dog 3',
'src' => 'https://www.placedog.net/120/100',
],
],
];
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['options']['#title'] = $this
->t('Image options');
$form['options']['images'] = [
'#title' => $this
->t('Images'),
'#type' => 'webform_image_select_element_images',
'#weight' => -10,
];
$form['options']['images_randomize'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Randomize images'),
'#description' => $this
->t('Randomizes the order of the images when they are displayed in the webform'),
'#return_value' => TRUE,
];
$form['options']['show_label'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show labels'),
'#description' => $this
->t('If checked, the image text will be displayed below each image.'),
'#return_value' => TRUE,
];
$form['options']['filter'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include filter by label'),
'#description' => $this
->t('If checked, users will be able search/filter images by their labels.'),
'#return_value' => TRUE,
'#states' => [
'visible' => [
':input[name="properties[show_label]"]' => [
'checked' => TRUE,
],
],
],
];
$form['options']['filter_container'] = [
'#type' => 'container',
'#attributes' => [
'data-webform-states-no-clear' => TRUE,
],
'#states' => [
'visible' => [
':input[name="properties[filter]"]' => [
'checked' => TRUE,
],
],
],
];
$form['options']['filter_container']['filter__placeholder'] = [
'#type' => 'textfield',
'#title' => $this
->t('Filter placeholder label'),
];
$form['options']['filter_container']['filter__singlular'] = [
'#type' => 'textfield',
'#title' => $this
->t('Filter single item label'),
];
$form['options']['filter_container']['filter__plural'] = [
'#type' => 'textfield',
'#title' => $this
->t('Filter plural items label'),
];
$form['options']['filter_container']['filter__no_results'] = [
'#type' => 'textfield',
'#title' => $this
->t('Filter no results label'),
];
return $form;
}
}