Skip to content
Snippets Groups Projects
Commit ae93615e authored by Frank Mably's avatar Frank Mably
Browse files

Merge branch '3479459-drupal-11-compatibility' into '2.0.x'

Issue #3479459 by mably: Drupal 11 compatibility fixes for select_text_value

See merge request !3
parents 6eb714c9 9b4b0db5
No related branches found
No related tags found
No related merge requests found
Pipeline #364842 passed
andyg
daggerhart
################
# GitLabCI template for Drupal projects.
#
# This template is designed to give any Contrib maintainer everything they need to test, without requiring modification.
# It is also designed to keep up to date with Core Development automatically through the use of include files that can be centrally maintained.
# As long as you include the project, ref and three files below, any future updates added by the Drupal Association will be used in your
# pipelines automatically. However, you can modify this template if you have additional needs for your project.
# The full documentation is on https://project.pages.drupalcode.org/gitlab_templates/
################
# For information on alternative values for 'ref' see https://project.pages.drupalcode.org/gitlab_templates/info/templates-version/
# To test a Drupal 7 project, change the first include filename from .main.yml to .main-d7.yml
include:
- project: $_GITLAB_TEMPLATES_REPO
ref: $_GITLAB_TEMPLATES_REF
file:
- "/includes/include.drupalci.main.yml"
- "/includes/include.drupalci.variables.yml"
- "/includes/include.drupalci.workflows.yml"
################
# Pipeline configuration variables are defined with default values and descriptions in the file
# https://git.drupalcode.org/project/gitlab_templates/-/blob/main/includes/include.drupalci.variables.yml
# Uncomment the lines below if you want to override any of the variables. The following is just an example.
################
# variables:
# SKIP_ESLINT: '1'
# OPT_IN_TEST_NEXT_MAJOR: '1'
# _CURL_TEMPLATES_REF: 'main'
#
# Start custom overrides.
# Based on https://git.drupalcode.org/project/keycdn/-/blob/8.x-1.x/.gitlab-ci.yml
#
variables:
OPT_IN_TEST_CURRENT: 0
OPT_IN_TEST_MAX_PHP: 0
OPT_IN_TEST_PREVIOUS_MAJOR: 1
OPT_IN_TEST_PREVIOUS_MINOR: 0
OPT_IN_TEST_NEXT_MINOR: 1
OPT_IN_TEST_NEXT_MAJOR: 0
# Select Text Value
This module provides additional widgets for the following field types:
- Text (formatted)
- Text (formatted, long)
- Text (plain)
- Text (plain, long)
The new widgets allow the site administrator to set some pre-defined values
for the text field that the user can then select from, using either a select
field or radio buttons.
Recommended that the "Allowed values" list does not use the key|label pattern
that is common with Drupal field settings. Instead, just provide one value per
line that will be both stored in the database and displayed on output.
The data is then saved to the database as the field would normally save it.
Similar to the "select_or_other" module, but with a fundamentally different
approach.
## Table of contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Configuration](#configuration)
- [Maintainers](#maintainers)
## Requirements
This module requires no modules outside of Drupal core.
## Installation
Install as you would normally install a contributed Drupal module. For further
information, see
[Installing Drupal Modules](https://www.drupal.org/docs/extending-drupal/installing-drupal-modules).
## Installation
Install as you would normally install a contributed Drupal module. Visit:
https://www.drupal.org/docs/extending-drupal/installing-drupal-modules for
further information.
## Configuration
Choose the Select Text Value Widget in the content type form display settings.
## Maintainers
- Andy Giles - [andyg5000](https://www.drupal.org/u/andyg5000)
- Jonathan Daggerhart - [daggerhart](https://www.drupal.org/u/daggerhart)
{
"name": "drupal/select_text_value",
"description": "Widget for text fields to offer pre-define values for selection.",
"type": "drupal-module",
"license": "GPL-2.0-or-later",
"minimum-stability": "dev",
"homepage": "https://drupal.org/project/select_text_value",
"support": {
"issues": "https://www.drupal.org/project/issues/select_text_value",
"source": "https://git.drupalcode.org/project/select_text_value"
}
}
name: 'Select Text Value'
type: module
description: 'Widget for text fields to offer pre-define values for selection'
core: 8.x
core_version_requirement: ^8 || ^9 || ^10
core_version_requirement: ^10.3 || ^11
<?php
/**
* @file
* Hook implementations for the select_text_value module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
......
......@@ -3,9 +3,9 @@
namespace Drupal\select_text_value\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\select_text_value\WidgetHelper;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\select_text_value\WidgetHelper;
use Drupal\text\Plugin\Field\FieldWidget\TextareaWidget;
use Symfony\Component\Validator\ConstraintViolationInterface;
......@@ -25,7 +25,7 @@ class SelectFormattedTextareaWidget extends TextareaWidget {
/**
* Common functionality shared between all widgets.
*
* @var WidgetHelper
* @var \Drupal\select_text_value\WidgetHelper
*/
protected $helper;
......@@ -95,6 +95,7 @@ class SelectFormattedTextareaWidget extends TextareaWidget {
/**
* {@inheritdoc}
*/
// phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found, Drupal.Commenting.FunctionComment.Missing
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
return parent::errorElement($element, $violation, $form, $form_state);
}
......
......@@ -3,9 +3,9 @@
namespace Drupal\select_text_value\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\select_text_value\WidgetHelper;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\select_text_value\WidgetHelper;
use Drupal\text\Plugin\Field\FieldWidget\TextfieldWidget;
use Symfony\Component\Validator\ConstraintViolationInterface;
......@@ -25,7 +25,7 @@ class SelectFormattedTextfieldWidget extends TextfieldWidget {
/**
* Common functionality shared between all widgets.
*
* @var WidgetHelper
* @var \Drupal\select_text_value\WidgetHelper
*/
protected $helper;
......@@ -95,6 +95,7 @@ class SelectFormattedTextfieldWidget extends TextfieldWidget {
/**
* {@inheritdoc}
*/
// phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found, Drupal.Commenting.FunctionComment.Missing
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
return parent::errorElement($element, $violation, $form, $form_state);
}
......
......@@ -3,10 +3,10 @@
namespace Drupal\select_text_value\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\select_text_value\WidgetHelper;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextareaWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\select_text_value\WidgetHelper;
use Symfony\Component\Validator\ConstraintViolationInterface;
/**
......@@ -25,7 +25,7 @@ class SelectStringTextareaWidget extends StringTextareaWidget {
/**
* Common functionality shared between all widgets.
*
* @var WidgetHelper
* @var \Drupal\select_text_value\WidgetHelper
*/
protected $helper;
......@@ -95,6 +95,7 @@ class SelectStringTextareaWidget extends StringTextareaWidget {
/**
* {@inheritdoc}
*/
// phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found, Drupal.Commenting.FunctionComment.Missing
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
return parent::errorElement($element, $violation, $form, $form_state);
}
......
......@@ -3,10 +3,10 @@
namespace Drupal\select_text_value\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\select_text_value\WidgetHelper;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\select_text_value\WidgetHelper;
use Symfony\Component\Validator\ConstraintViolationInterface;
/**
......@@ -25,7 +25,7 @@ class SelectStringTextfieldWidget extends StringTextfieldWidget {
/**
* Common functionality shared between all widgets.
*
* @var WidgetHelper
* @var \Drupal\select_text_value\WidgetHelper
*/
protected $helper;
......@@ -95,6 +95,7 @@ class SelectStringTextfieldWidget extends StringTextfieldWidget {
/**
* {@inheritdoc}
*/
// phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found, Drupal.Commenting.FunctionComment.Missing
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
return parent::errorElement($element, $violation, $form, $form_state);
}
......
......@@ -8,7 +8,7 @@ use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Class WidgetHelper.
* Various widget helper functions for the select_text_value module.
*
* @package Drupal\select_text_value
*/
......@@ -20,7 +20,7 @@ class WidgetHelper {
* Simple array of default values for all form widgets.
*
* @return array
* Shared settings across all widgets.
* Shared settings across all widgets.
*/
public static function defaultSettings() {
return [
......@@ -114,14 +114,14 @@ class WidgetHelper {
/**
* Adds the custom value option to the select element.
*
* @param array $select form element.
*
* @param array $select
* The select element.
* @param array $settings
* Widget settings.
*/
public function setFormElementSelectCustomOption(array &$select, $settings) {
$select['#options'] += [
'_custom_value' => !empty($settings['custom_value_label']) ? $settings['custom_value_label'] : $this->t('Other')
'_custom_value' => !empty($settings['custom_value_label']) ? $settings['custom_value_label'] : $this->t('Other'),
];
}
......@@ -154,8 +154,10 @@ class WidgetHelper {
/**
* Move the original form element #required property to the select field.
*
* The custom value field cannot easily be required from Drupal's perspective.
* @todo - Handle form #required this with custom validation on the form.
*
* @todo Handle form #required this with custom validation on the form.
*
* @param array $element
* The widget's full render element.
......@@ -216,7 +218,7 @@ class WidgetHelper {
/**
* Set the default values when the form element is loaded.
*
* @param FieldItemListInterface $items
* @param \Drupal\Core\Field\FieldItemListInterface $items
* Item values for the field.
* @param int $delta
* Which item is being rendered.
......@@ -234,8 +236,9 @@ class WidgetHelper {
if (!empty($items[$delta]->value)) {
$element['select']['#default_value'] = '_custom_value';
if (in_array($items[$delta]->value, $allowed_values)) {
$element['select']['#default_value'] = $items[$delta]->value;
$value = $items[$delta]->value;
if (array_key_exists($value, $allowed_values) || in_array($value, $allowed_values)) {
$element['select']['#default_value'] = $value;
$element['field']['#default_value'] = '';
}
else {
......@@ -265,7 +268,7 @@ class WidgetHelper {
foreach ($lines as $line) {
$line = trim($line);
if (strpos($line, '|') !== FALSE) {
list($key, $value) = explode('|', $line);
[$key, $value] = explode('|', $line);
$allowed_values[trim($key)] = trim($value);
}
else {
......@@ -283,10 +286,10 @@ class WidgetHelper {
* HTML description for allowed values field.
*/
public function allowedValuesDescription() {
$description = '<p>' . t('The possible values this field can contain. Enter one value per line, in the format key|label.');
$description .= '<br/>' . t('The key is the stored value, and must be numeric. The label will be used in displayed values and edit forms.');
$description .= '<br/>' . t('The label is optional: if a line contains a single number, it will be used as key and label.');
$description .= '<br/>' . t('Lists of labels are also accepted (one label per line), only if the field does not hold any values yet. Numeric keys will be automatically generated from the positions in the list.');
$description = '<p>' . $this->t('The possible values this field can contain. Enter one value per line, in the format key|label.');
$description .= '<br/>' . $this->t('The key is the stored value, and must be numeric. The label will be used in displayed values and edit forms.');
$description .= '<br/>' . $this->t('The label is optional: if a line contains a single number, it will be used as key and label.');
$description .= '<br/>' . $this->t('Lists of labels are also accepted (one label per line), only if the field does not hold any values yet. Numeric keys will be automatically generated from the positions in the list.');
$description .= '</p>';
return $description;
}
......@@ -298,7 +301,7 @@ class WidgetHelper {
* Entire field element, consisting of multiple values.
*
* @return array
* Altered element with States.
* Altered element with States.
*/
public static function stringFieldAssignStates(array $element) {
foreach (Element::children($element) as $delta) {
......@@ -324,7 +327,7 @@ class WidgetHelper {
* Entire field element, consisting of multiple values.
*
* @return array
* Altered element with States.
* Altered element with States.
*/
public static function formattedTextFieldAssignStates(array $element) {
foreach (Element::children($element) as $delta) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment