Commit 38fca3a7 authored by catch's avatar catch

Issue #2745491 by mcdruid, juampynr, Jo Fitzgerald, sanduhrs, snehi,...

Issue #2745491 by mcdruid, juampynr, Jo Fitzgerald, sanduhrs, snehi, mparker17, rodrigoaguilera: ImageWidget::validateRequiredFields() produces a PHP Warning message if triggering element is a non-button
parent 1bc9459a
......@@ -295,7 +295,8 @@ public static function process($element, FormStateInterface $form_state, $form)
public static function validateRequiredFields($element, FormStateInterface $form_state) {
// Only do validation if the function is triggered from other places than
// the image process form.
if (!in_array('file_managed_file_submit', $form_state->getTriggeringElement()['#submit'])) {
$triggering_element = $form_state->getTriggeringElement();
if (empty($triggering_element['#submit']) || !in_array('file_managed_file_submit', $triggering_element['#submit'])) {
// If the image is not there, we do not check for empty values.
$parents = $element['#parents'];
$field = array_pop($parents);
......
......@@ -2,6 +2,9 @@
namespace Drupal\image\Tests;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
/**
* Tests validation functions such as min/max resolution.
*
......@@ -238,4 +241,48 @@ public function testAJAXValidationMessage() {
$this->assertEqual(count($elements), 1, 'Ajax validation messages are displayed once.');
}
/**
* Tests that image field validation works with other form submit handlers.
*/
public function testFriendlyAjaxValidation() {
// Add a custom field to the Article content type that contains an AJAX
// handler on a select field.
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_dummy_select',
'type' => 'image_module_test_dummy_ajax',
'entity_type' => 'node',
'cardinality' => 1,
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'node',
'bundle' => 'article',
'field_name' => 'field_dummy_select',
'label' => t('Dummy select'),
])->save();
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.article.default')
->setComponent(
'field_dummy_select',
[
'type' => 'image_module_test_dummy_ajax_widget',
'weight' => 1,
])
->save();
// Then, add an image field.
$this->createImageField('field_dummy_image', 'article');
// Open an article and trigger the AJAX handler.
$this->drupalGet('node/add/article');
$edit = [
'field_dummy_select[select_widget]' => 'bam',
];
$this->drupalPostAjaxForm(NULL, $edit, 'field_dummy_select[select_widget]');
}
}
<?php
namespace Drupal\image_module_test\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
/**
* Empty renderer for a dummy field with an AJAX handler.
*
* @FieldFormatter(
* id = "image_module_test_dummy_ajax_formatter",
* module = "image_module_test",
* label = @Translation("Dummy AJAX"),
* field_types= {
* "image_module_test_dummy_ajax"
* }
* )
*/
class DummyAjaxFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = t('Renders nothing');
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
return $element;
}
}
<?php
namespace Drupal\image_module_test\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Defines a dummy field containing an AJAX handler.
*
* @FieldType(
* id = "image_module_test_dummy_ajax",
* label = @Translation("Dummy AJAX"),
* description = @Translation("A field containing an AJAX handler."),
* category = @Translation("Field"),
* default_widget = "image_module_test_dummy_ajax_widget",
* default_formatter = "image_module_test_dummy_ajax_formatter"
* )
*/
class DummyAjaxItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 255,
],
],
];
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
return empty($this->get('value')->getValue());
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Dummy string value'));
return $properties;
}
}
<?php
namespace Drupal\image_module_test\Plugin\Field\FieldWidget;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Default widget for Dummy AJAX test.
*
* @FieldWidget(
* id = "image_module_test_dummy_ajax_widget",
* label = @Translation("Dummy AJAX widget"),
* field_types = {
* "image_module_test_dummy_ajax"
* },
* multiple_values = TRUE,
* )
*/
class DummyAjaxWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['select_widget'] = [
'#type' => 'select',
'#title' => $this->t('Dummy select'),
'#options' => ['pow' => 'Pow!', 'bam' => 'Bam!'],
'#required' => TRUE,
'#ajax' => [
'callback' => get_called_class() . '::dummyAjaxCallback',
'effect' => 'fade',
],
];
return $element;
}
/**
* Ajax callback for Dummy AJAX test.
*
* @param array $form
* The build form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* Ajax response.
*/
public static function dummyAjaxCallback(array &$form, FormStateInterface $form_state) {
return new AjaxResponse();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment