Skip to content
Snippets Groups Projects
Commit c667094d authored by Bryan Heisler's avatar Bryan Heisler
Browse files

Resolve #3418165 "Support emptyoption for"

parent e9c0d580
No related branches found
No related tags found
1 merge request!2Resolve #3418165 "Support emptyoption for"
services:
php:
# Specify the version of Drupal you wish to use for Tugboat below.
image: q0rban/tugboat-drupal:10
default: true
http: false
depends: mysql
commands:
update: |
set -eux
# Check out a branch using the unique Tugboat ID for this repository, to
# ensure we don't clobber an existing branch.
git checkout -b $TUGBOAT_REPO_ID
# Composer is hungry. You need a Tugboat project with a pretty sizeable
# chunk of memory.
export COMPOSER_MEMORY_LIMIT=-1
# This is an environment variable we added in the Dockerfile that
# provides the path to Drupal composer root (not the web root).
cd $DRUPAL_COMPOSER_ROOT
# If you need to change the minimum stability due to downstream
# dependencies, you can modify 'stable' below to your needs:
# see https://getcomposer.org/doc/04-schema.md#minimum-stability
composer config minimum-stability stable
# We configure the Drupal project to use the checkout of the module as a
# Composer package repository.
composer config repositories.tugboat vcs $TUGBOAT_ROOT
# Now we can require this module, specifing the branch name we created
# above that uses the $TUGBOAT_REPO_ID environment variable.
composer require drupal/typed_link:dev-$TUGBOAT_REPO_ID
# Install Drupal on the site.
vendor/bin/drush \
--yes \
--db-url=mysql://tugboat:tugboat@mysql:3306/tugboat \
--site-name="Live preview for ${TUGBOAT_PREVIEW_NAME}" \
--account-pass=admin \
site:install standard
# Set up the files directory permissions.
mkdir -p $DRUPAL_DOCROOT/sites/default/files
chgrp -R www-data $DRUPAL_DOCROOT/sites/default/files
chmod 2775 $DRUPAL_DOCROOT/sites/default/files
chmod -R g+w $DRUPAL_DOCROOT/sites/default/files
# Enable the module.
vendor/bin/drush --yes pm:enable typed_link
build: |
set -eux
# Delete and re-check out this branch in case this is built from a Base Preview.
git branch -D $TUGBOAT_REPO_ID && git checkout -b $TUGBOAT_REPO_ID || true
export COMPOSER_MEMORY_LIMIT=-1
cd $DRUPAL_COMPOSER_ROOT
composer install --optimize-autoloader
# Update this module, including all dependencies.
composer update drupal/typed_link --with-all-dependencies
vendor/bin/drush --yes updb
vendor/bin/drush cache:rebuild
mysql:
image: tugboatqa/mariadb
......@@ -24,7 +24,7 @@ class TypedLinkFormatter extends LinkFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
public function viewElements(FieldItemListInterface $items, $langcode): array {
$elements = parent::viewElements($items, $langcode);
// Only collect allowed options if there are actually items to display.
......@@ -39,7 +39,7 @@ class TypedLinkFormatter extends LinkFormatter {
$value = $item->link_type;
// If the stored value is in the current set of allowed values, display
// the associated label, otherwise just display the raw value.
$output = isset($options[$value]) ? $options[$value] : $value;
$output = $options[$value] ?? $value;
$elements[$delta]['type'] = [
'#markup' => $output,
'#allowed_tags' => FieldFilteredMarkup::allowedTags(),
......
......@@ -29,11 +29,11 @@ use Drupal\options\Plugin\Field\FieldType\ListStringItem;
class TypedLinkItem extends LinkItem implements OptionsProviderInterface {
/**
* An string option field.
* A string option field.
*
* @var \Drupal\options\Plugin\Field\FieldType\ListStringItem
*/
protected $optionField;
protected ListStringItem $optionField;
/**
* {@inheritdoc}
......@@ -51,7 +51,7 @@ class TypedLinkItem extends LinkItem implements OptionsProviderInterface {
$properties['link_type'] = DataDefinition::create('string')
->setLabel(t('Link Type'))
->addConstraint('Length', ['max' => 255])
->setRequired(TRUE);
->setRequired(FALSE);
return $properties;
}
......
......@@ -23,16 +23,21 @@ use Drupal\link\Plugin\Field\FieldWidget\LinkWidget;
*/
class TypedLinkWidget extends LinkWidget {
private $column;
private $required;
private $options;
/**
* Widget options.
*
* @var array
*/
private array $options;
/**
* {@inheritdoc}
* Get the column name of the field.
*
* @return string
* The column name.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->column = $field_definition->getFieldStorageDefinition()->getMainPropertyName();
private function getColumn(): string {
return $this->fieldDefinition->getFieldStorageDefinition()->getMainPropertyName();
}
/**
......@@ -40,15 +45,22 @@ class TypedLinkWidget extends LinkWidget {
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$this->required = $element['#required'];
$element['link_type'] = [
'#title' => $this->t('Link type'),
'#type' => 'select',
'#options' => $this->getOptions($items->getEntity()),
'#default_value' => $this->getSelectedOption($items[$delta]),
// Do not display a 'multiple' select box if there is only one option.
'#empty_option' => $this->t('- Select -'),
'#multiple' => FALSE,
'#required' => $element['#required'],
'#states' => [
'required' => [
':input[name="' . $items->getName() . '[' . $delta . '][uri]' . '"]' => [
'filled' => TRUE,
],
],
],
];
return $element;
......@@ -68,7 +80,7 @@ class TypedLinkWidget extends LinkWidget {
// Limit the settable options for the current user account.
$options = $this->fieldDefinition
->getFieldStorageDefinition()
->getOptionsProvider($this->column, $entity)
->getOptionsProvider($this->getColumn(), $entity)
->getSettableOptions(\Drupal::currentUser());
$module_handler = \Drupal::moduleHandler();
......@@ -100,7 +112,7 @@ class TypedLinkWidget extends LinkWidget {
// We need to check against a flat list of options.
$flat_options = OptGroup::flattenOptions($this->getOptions($item->getEntity()));
$value = $item->{$this->column};
$value = $item->{$this->getColumn()};
$selected_option = NULL;
// Keep the value if it actually is in the list of options (needs to be
// checked against the flat list).
......@@ -112,7 +124,10 @@ class TypedLinkWidget extends LinkWidget {
}
/**
* {@inheritdoc}
* Sanitize label.
*
* @param string $label
* The label.
*/
protected function sanitizeLabel(&$label) {
// Select form inputs allow unencoded HTML entities, but no HTML tags.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment