Skip to content
Snippets Groups Projects
Commit 3a05f4b9 authored by M Parker's avatar M Parker
Browse files

Fix coding standards messages.

parent f1171003
No related branches found
No related tags found
No related merge requests found
...@@ -41,13 +41,13 @@ div.multiselect-selected { ...@@ -41,13 +41,13 @@ div.multiselect-selected {
} }
.multiselect-btns > ul { .multiselect-btns > ul {
float: left; float: left;
margin: 10px 10px 0px 20px; margin: 10px 10px 0 20px;
padding: 0px; padding: 0;
list-style: none; list-style: none;
} }
[dir="rtl"] .multiselect-btns > ul { [dir="rtl"] .multiselect-btns > ul {
float: right; float: right;
margin: 10px 20px 0px 10px; margin: 10px 20px 0 10px;
} }
.multiselect-btns > ul > li.multiselect-add, .multiselect-btns > ul > li.multiselect-add,
.multiselect-btns > ul > li.multiselect-remove { .multiselect-btns > ul > li.multiselect-remove {
...@@ -75,7 +75,7 @@ div.multiselect-selected { ...@@ -75,7 +75,7 @@ div.multiselect-selected {
.multiselect-btns > ul > li.multiselect-remove:hover { .multiselect-btns > ul > li.multiselect-remove:hover {
background-position: -33px 0; background-position: -33px 0;
} }
.multiselect-btns > ul > li.mu^^ltiselect-add:active, .multiselect-btns > ul > li.multiselect-add:active,
.multiselect-btns > ul > li.multiselect-remove:active { .multiselect-btns > ul > li.multiselect-remove:active {
background-position: -66px 0; background-position: -66px 0;
} }
...@@ -99,7 +99,8 @@ div.multiselect-selected { ...@@ -99,7 +99,8 @@ div.multiselect-selected {
left: 35%; left: 35%;
top: 10px; top: 10px;
} }
.multiselect-available.form-multiselect option, .multiselect-selected.form-multiselect option { .multiselect-available.form-multiselect option,
.multiselect-selected.form-multiselect option {
padding: 15px; padding: 15px;
} }
.multiselect-btns > ul > li.multiselect-add { .multiselect-btns > ul > li.multiselect-add {
...@@ -108,7 +109,8 @@ div.multiselect-selected { ...@@ -108,7 +109,8 @@ div.multiselect-selected {
.multiselect-btns > ul > li.multiselect-remove { .multiselect-btns > ul > li.multiselect-remove {
background: url(../images/remove-mobile.png) no-repeat 0 0; background: url(../images/remove-mobile.png) no-repeat 0 0;
} }
.multiselect-btns > ul > li.multiselect-add, .multiselect-btns > ul > li.multiselect-remove { .multiselect-btns > ul > li.multiselect-add,
.multiselect-btns > ul > li.multiselect-remove {
margin-right: 50px; margin-right: 50px;
width: 35px; width: 35px;
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/** /**
* @file * @file
* Allows users to select multiple items in an easier way than the normal node-reference widget. * Select multiple items in an easier way than the normal node-reference widget.
*/ */
use Drupal\Core\Render\Element; use Drupal\Core\Render\Element;
...@@ -34,20 +34,20 @@ function multiselect_help($route_name, RouteMatchInterface $route_match) { ...@@ -34,20 +34,20 @@ function multiselect_help($route_name, RouteMatchInterface $route_match) {
*/ */
function multiselect_page_attachments(&$attachments) { function multiselect_page_attachments(&$attachments) {
$config = \Drupal::config('multiselect.settings'); $config = \Drupal::config('multiselect.settings');
$attachments['#attached']['drupalSettings']['multiselect'] = array('widths' => $config->get('multiselect.widths')); $attachments['#attached']['drupalSettings']['multiselect'] = ['widths' => $config->get('multiselect.widths')];
} }
/** /**
* Implements hook_theme(). * Implements hook_theme().
*/ */
function multiselect_theme() { function multiselect_theme() {
return array( return [
'multiselect' => array( 'multiselect' => [
'arguments' => array('element' => NULL), 'arguments' => ['element' => NULL],
'render element' => 'element', 'render element' => 'element',
'template' => 'multiselect', 'template' => 'multiselect',
), ],
); ];
} }
/** /**
...@@ -55,15 +55,15 @@ function multiselect_theme() { ...@@ -55,15 +55,15 @@ function multiselect_theme() {
* *
* Default template: multiselect.html.twig. * Default template: multiselect.html.twig.
* *
* @param $variables * @param array &$variables
* An associative array containing: * An associative array containing:
* - element: An associative array containing the properties of the element. * - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #extra, * Properties used: #title, #value, #options, #description, #extra,
* #multiple, #required, #name, #attributes, #size. * #multiple, #required, #name, #attributes, #size.
*/ */
function template_preprocess_multiselect(&$variables) { function template_preprocess_multiselect(array &$variables) {
$element = $variables['element']; $element = $variables['element'];
Element::setAttributes($element, array('id', 'name', 'size', 'required')); Element::setAttributes($element, ['id', 'name', 'size', 'required']);
$available_options = Multiselect::getOptions('available', $element); $available_options = Multiselect::getOptions('available', $element);
$available_size = min(count($available_options), 10); $available_size = min(count($available_options), 10);
...@@ -72,33 +72,33 @@ function template_preprocess_multiselect(&$variables) { ...@@ -72,33 +72,33 @@ function template_preprocess_multiselect(&$variables) {
$selected_size = min(count($selected_options), 10); $selected_size = min(count($selected_options), 10);
$total_size = $available_size + $selected_size; $total_size = $available_size + $selected_size;
$variables['multiselect'] = array( $variables['multiselect'] = [
'available' => array( 'available' => [
'id' => $element['#attributes']['id'] . '-available', 'id' => $element['#attributes']['id'] . '-available',
'label' => t('Available Options'), 'label' => t('Available Options'),
'attributes' => array( 'attributes' => [
'id' => $element['#attributes']['id'] . '-available', 'id' => $element['#attributes']['id'] . '-available',
'size' => $total_size, 'size' => $total_size,
), ],
'options' => $available_options, 'options' => $available_options,
), ],
'selected' => array( 'selected' => [
'id' => $element['#attributes']['id'], 'id' => $element['#attributes']['id'],
'label' => t('Selected Options'), 'label' => t('Selected Options'),
'attributes' => $element['#attributes'], 'attributes' => $element['#attributes'],
'options' => $selected_options, 'options' => $selected_options,
), ],
'labels' => array( 'labels' => [
'add' => t('Add'), 'add' => t('Add'),
'remove' => t('Remove'), 'remove' => t('Remove'),
), ],
); ];
// Prepare selected attributes. // Prepare selected attributes.
$variables['multiselect']['selected']['attributes']['size'] = $total_size; $variables['multiselect']['selected']['attributes']['size'] = $total_size;
// Prepare attributes for available select. // Prepare attributes for available select.
foreach (array('multiple', 'class') as $key) { foreach (['multiple', 'class'] as $key) {
$element_key = "#{$key}"; $element_key = "#{$key}";
if (isset($element[$element_key])) { if (isset($element[$element_key])) {
$variables['multiselect']['available']['attributes'][$key] = $element[$element_key]; $variables['multiselect']['available']['attributes'][$key] = $element[$element_key];
...@@ -106,7 +106,7 @@ function template_preprocess_multiselect(&$variables) { ...@@ -106,7 +106,7 @@ function template_preprocess_multiselect(&$variables) {
} }
// Prepare attributes. // Prepare attributes.
$multiselect = &$variables['multiselect']; $multiselect = &$variables['multiselect'];
foreach (array('available', 'selected') as $key) { foreach (['available', 'selected'] as $key) {
$multiselect[$key]['attributes']['class'][] = 'multiselect-' . $key; $multiselect[$key]['attributes']['class'][] = 'multiselect-' . $key;
$multiselect[$key]['attributes']['class'][] = 'form-multiselect'; $multiselect[$key]['attributes']['class'][] = 'form-multiselect';
if (isset($multiselect[$key]['attributes']) && !($multiselect[$key]['attributes'] instanceof Attribute)) { if (isset($multiselect[$key]['attributes']) && !($multiselect[$key]['attributes'] instanceof Attribute)) {
...@@ -116,4 +116,3 @@ function template_preprocess_multiselect(&$variables) { ...@@ -116,4 +116,3 @@ function template_preprocess_multiselect(&$variables) {
} }
} }
} }
<?php <?php
/**
* @file
* Contains \Drupal\multiselect\Element\Multiselect.
*/
namespace Drupal\multiselect\Element; namespace Drupal\multiselect\Element;
use Drupal\Core\Render\Element\Select; use Drupal\Core\Render\Element\Select;
/** /**
...@@ -23,19 +19,19 @@ class Multiselect extends Select { ...@@ -23,19 +19,19 @@ class Multiselect extends Select {
*/ */
public function getInfo() { public function getInfo() {
$class = get_class($this); $class = get_class($this);
return array( return [
'#theme' => 'multiselect', '#theme' => 'multiselect',
'#input' => TRUE, '#input' => TRUE,
'#multiple' => TRUE, '#multiple' => TRUE,
'#process' => array( '#process' => [
array($class, 'processSelect'), [$class, 'processSelect'],
), ],
'#default_value' => NULL, '#default_value' => NULL,
'#theme_wrappers' => array('form_element'), '#theme_wrappers' => ['form_element'],
'#attached' => array( '#attached' => [
'library' => array('multiselect/drupal.multiselect'), 'library' => ['multiselect/drupal.multiselect'],
), ],
); ];
} }
/** /**
...@@ -43,9 +39,9 @@ class Multiselect extends Select { ...@@ -43,9 +39,9 @@ class Multiselect extends Select {
* *
* @param string $type * @param string $type
* The type of options. * The type of options.
* @param $element * @param array $element
* An associative array containing the properties of the element. * An associative array containing the properties of the element.
* @param $choices * @param mixed $choices
* Mixed: Either an associative array of items to list as choices, or an * Mixed: Either an associative array of items to list as choices, or an
* object with an 'option' member that is an associative array. This * object with an 'option' member that is an associative array. This
* parameter is only used internally and should not be passed. * parameter is only used internally and should not be passed.
...@@ -53,18 +49,18 @@ class Multiselect extends Select { ...@@ -53,18 +49,18 @@ class Multiselect extends Select {
* @return array * @return array
* An array of options for the multiselect form element. * An array of options for the multiselect form element.
*/ */
public static function getOptions($type, $element, $choices = NULL) { public static function getOptions($type, array $element, $choices = NULL) {
if (!isset($choices)) { if (!isset($choices)) {
if (empty($element['#options'])) { if (empty($element['#options'])) {
return array(); return [];
} }
$choices = $element['#options']; $choices = $element['#options'];
} }
$options = array(); $options = [];
// array_key_exists() accommodates the rare event where $element['#value'] is NULL. // array_key_exists() accommodates the rare event where $element['#value']
// isset() fails in this situation. // is NULL. isset() fails in this situation.
$value_valid = isset($element['#value']) || array_key_exists('#value', $element); $value_valid = isset($element['#value']) || array_key_exists('#value', $element);
$value_is_array = $value_valid && is_array($element['#value']); $value_is_array = $value_valid && is_array($element['#value']);
// Check if the element is multiple select and no value has been selected. // Check if the element is multiple select and no value has been selected.
...@@ -83,14 +79,21 @@ class Multiselect extends Select { ...@@ -83,14 +79,21 @@ class Multiselect extends Select {
switch ($type) { switch ($type) {
case 'available': case 'available':
if (!($value_valid && ((!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value']))) || $empty_choice))) { if (!($value_valid && ((!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value']))) || $empty_choice))) {
$options[] = array('type' => 'option', 'value' => $key, 'label' => $choice); $options[] = [
'type' => 'option',
'value' => $key,
'label' => $choice,
];
} }
break; break;
case 'selected': case 'selected':
if ($value_valid && ((!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value']))) || $empty_choice)) { if ($value_valid && ((!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value']))) || $empty_choice)) {
//$options .= '<option value="' . Html::escape($key) . '" selected="selected">' . Html::escape($choice) . '</option>'; $options[] = [
$options[] = array('type' => 'option', 'value' => $key, 'label' => $choice); 'type' => 'option',
'value' => $key,
'label' => $choice,
];
} }
break; break;
} }
...@@ -98,4 +101,5 @@ class Multiselect extends Select { ...@@ -98,4 +101,5 @@ class Multiselect extends Select {
} }
return $options; return $options;
} }
} }
<?php <?php
/**
* @file
* Contains \Drupal\multiselect\Form\SettingsForm.
*/
namespace Drupal\multiselect\Form; namespace Drupal\multiselect\Form;
use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\ConfigFormBase;
...@@ -30,15 +25,15 @@ class SettingsForm extends ConfigFormBase { ...@@ -30,15 +25,15 @@ class SettingsForm extends ConfigFormBase {
$config = $this->config('multiselect.settings'); $config = $this->config('multiselect.settings');
$form['basic'] = array(); $form['basic'] = [];
$form['basic']['multiselect_widths'] = array( $form['basic']['multiselect_widths'] = [
'#type' => 'textfield', '#type' => 'textfield',
'#title' => $this->t('Width of Select Boxes (in pixels)'), '#title' => $this->t('Width of Select Boxes (in pixels)'),
'#default_value' => $config->get('multiselect.widths'), '#default_value' => $config->get('multiselect.widths'),
'#size' => 3, '#size' => 3,
'#field_suffix' => 'px', '#field_suffix' => 'px',
'#required' => TRUE, '#required' => TRUE,
); ];
return $form; return $form;
} }
......
<?php <?php
/**
* @file
* Contains \Drupal\multiselect\Plugin\Field\FieldWidget\MultiselectWidget.
*/
namespace Drupal\multiselect\Plugin\Field\FieldWidget; namespace Drupal\multiselect\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase; use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Select;
use Drupal\multiselect\Element\Multiselect;
/** /**
* Plugin implementation of the 'multiselect' widget. * Plugin implementation of the 'multiselect' widget.
...@@ -30,19 +22,21 @@ use Drupal\multiselect\Element\Multiselect; ...@@ -30,19 +22,21 @@ use Drupal\multiselect\Element\Multiselect;
* ) * )
*/ */
class MultiselectWidget extends OptionsWidgetBase { class MultiselectWidget extends OptionsWidgetBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
/** @var Multiselect $element */ /** @var \Drupal\multiselect\Element\Multiselect $element */
$element = parent::formElement($items, $delta, $element, $form, $form_state); $element = parent::formElement($items, $delta, $element, $form, $form_state);
// Prepare some properties for the child methods to build the actual form element. // Prepare some properties for the child methods to build the actual form
// element.
$this->required = $element['#required']; $this->required = $element['#required'];
$this->multiple = $this->fieldDefinition->getFieldStorageDefinition()->isMultiple(); $this->multiple = $this->fieldDefinition->getFieldStorageDefinition()->isMultiple();
$this->has_value = isset($items[0]->{$this->column}); $this->has_value = isset($items[0]->{$this->column});
$element += array( $element += [
'#type' => 'multiselect', '#type' => 'multiselect',
'#name' => $items->getName(), '#name' => $items->getName(),
'#size' => $this->getSetting('size'), '#size' => $this->getSetting('size'),
...@@ -50,7 +44,7 @@ class MultiselectWidget extends OptionsWidgetBase { ...@@ -50,7 +44,7 @@ class MultiselectWidget extends OptionsWidgetBase {
'#default_value' => $this->getSelectedOptions($items, $delta), '#default_value' => $this->getSelectedOptions($items, $delta),
// Do not display a 'multiple' select box if there is only one option. // Do not display a 'multiple' select box if there is only one option.
'#multiple' => $this->multiple && count($this->options) > 1, '#multiple' => $this->multiple && count($this->options) > 1,
); ];
return $element; return $element;
} }
...@@ -59,22 +53,22 @@ class MultiselectWidget extends OptionsWidgetBase { ...@@ -59,22 +53,22 @@ class MultiselectWidget extends OptionsWidgetBase {
* *
* @param array $element * @param array $element
* The form element. * The form element.
* @param array $form_state * @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state. * The form state.
*/ */
public static function validateElement(array $element, FormStateInterface $form_state) { public static function validateElement(array $element, FormStateInterface $form_state) {
// @todo fix multiselect validation. // @todo fix multiselect validation.
parent::validateElement($element, $form_state); parent::validateElement($element, $form_state);
// Massage submitted form values. // Massage submitted form values.
// Drupal\Core\Field\WidgetBase::submit() expects values as // Drupal\Core\Field\WidgetBase::submit() expects values as
// an array of values keyed by delta first, then by column, while our // an array of values keyed by delta first, then by column, while our
// widgets return the opposite. // widgets return the opposite.
if (is_array($element['#value'])) { if (is_array($element['#value'])) {
$values = array_values($element['#value']); $values = array_values($element['#value']);
} }
else { else {
$values = array($element ['#value']); $values = [$element['#value']];
} }
// Filter out the 'none' option. Use a strict comparison, because // Filter out the 'none' option. Use a strict comparison, because
...@@ -85,9 +79,9 @@ class MultiselectWidget extends OptionsWidgetBase { ...@@ -85,9 +79,9 @@ class MultiselectWidget extends OptionsWidgetBase {
} }
// Transpose selections from field => delta to delta => field. // Transpose selections from field => delta to delta => field.
$items = array(); $items = [];
foreach ($values as $value) { foreach ($values as $value) {
$items [] = array($element ['#key_column'] => $value); $items[] = [$element['#key_column'] => $value];
} }
$form_state->setValueForElement($element, $items); $form_state->setValueForElement($element, $items);
} }
......
...@@ -102,11 +102,11 @@ class MultiselectElementJavascriptTest extends WebDriverTestBase { ...@@ -102,11 +102,11 @@ class MultiselectElementJavascriptTest extends WebDriverTestBase {
* *
* @param string $selector * @param string $selector
* The CSS selector of the select element that contains the options. * The CSS selector of the select element that contains the options.
* @param $options * @param array $options
* An array of options where the array keys are the option values and the * An array of options where the array keys are the option values and the
* array values are the option labels. * array values are the option labels.
*/ */
protected function assertOptions($selector, $options) { protected function assertOptions($selector, array $options) {
$this->assertSession()->elementsCount('css', "$selector option", count($options)); $this->assertSession()->elementsCount('css', "$selector option", count($options));
foreach ($options as $value => $label) { foreach ($options as $value => $label) {
$option_selector = "$selector option[value=$value]"; $option_selector = "$selector option[value=$value]";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment