Repository URL to install this package:
|
Version:
1.3.12 ▾
|
<?php
namespace Drupal\dds_content\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\link\Plugin\Field\FieldWidget\LinkWidget;
use function GuzzleHttp\Promise\all;
/**
* Plugin implementation of the DDS Button widget.
*
* @FieldWidget(
* id = "dds_button",
* label = @Translation("DDS Button"),
* field_types = {
* "dds_button"
* }
* )
*/
class DDSButtonWidget extends LinkWidget {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings = parent::defaultSettings();
$settings['target_blank'] = 0;
return $settings;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$widget = parent::formElement($items, $delta, $element, $form, $form_state);
$value = $items[$delta]->getValue();
$widget['uri']['#type'] = 'dds_content_entity_autocomplete';
$widget['uri']['#target_type'] = 'multiple';
$widget['uri']['#target_types'] = ['node', 'media'];
$uri = isset($value['uri']) ? $value['uri'] : NULL;
if($uri) {
$uri = str_replace('internal:', '', $uri);
}
$widget['uri']['#default_value'] = $uri;
$widget['button_style'] = [
'#type' => 'select',
'#options' => $this->getSelectOptions($this->fieldDefinition->getSetting('allowed_values')),
'#title' => $this->t('Button style'),
'#default_value' => isset($value['button_style']) ? $value['button_style'] : null,
];
$widget['target_blank'] = [
'#type' => 'checkbox',
'#default_value' => !empty($value['target_blank']) ? $value['target_blank'] : 0,
'#title' => $this->t('Open in new window')
];
return $widget;
}
public function getSelectOptions(string $allowed_values) {
$options = [];
foreach(preg_split("/((\r?\n)|(\r\n?))/", $allowed_values) as $value){
$parts = explode('|', $value);
if(count($parts) == 2) {
$options[$parts[0]] = $parts[1];
}
}
return $options;
}
}