Commit 40d3d3f2 authored by catch's avatar catch

Issue #2155785 by alexpott, vijaycs85: Image_field_instance_update() can not...

Issue #2155785 by alexpott, vijaycs85: Image_field_instance_update() can not handle default images with a fid greater than 9.
parent fdba1d63
......@@ -420,10 +420,8 @@ function image_entity_presave(EntityInterface $entity, $type) {
return;
}
if (!empty($entity->settings['default_image']['fid'][0])) {
$entity->settings['default_image']['fid'] = $entity->settings['default_image']['fid'][0];
}
if ($fid = $entity->settings['default_image']['fid']) {
$fid = $entity->settings['default_image']['fid'];
if ($fid) {
$original_fid = isset($entity->original) ? $entity->original->settings['default_image']['fid'] : NULL;
if ($fid != $original_fid) {
$image = \Drupal::service('image.factory')->get(file_load($fid)->getFileUri());
......@@ -454,15 +452,10 @@ function image_field_entity_update(FieldInterface $field) {
$prior_field = $field->original;
// The value of a managed_file element can be an array if #extended == TRUE.
$fid_new = isset($field->settings['default_image']['fid']['fids']) ? $field->settings['default_image']['fid']['fids'] : $field->settings['default_image']['fid'];
$fid_old = isset($prior_field->settings['default_image']['fid']['fids']) ? $prior_field->settings['default_image']['fid']['fids'] : $prior_field->settings['default_image']['fid'];
// Ensure that $fid_new and $fid_old are arrays, because the field setting
// 'default_image' key 'fid' might be the fallback value 0, see the annotation
// block of \Drupal\image\Plugin\Field\FieldType\ImageItem.
$fid_old = (array) $fid_old;
$fid_new = (array) $fid_new;
$fid_new = $field->settings['default_image']['fid'];
$fid_old = $prior_field->settings['default_image']['fid'];
$file_new = $fid_new ? file_load(reset($fid_new)) : FALSE;
$file_new = $fid_new ? file_load($fid_new) : FALSE;
if ($fid_new != $fid_old) {
......@@ -474,7 +467,7 @@ function image_field_entity_update(FieldInterface $field) {
}
// Is there an old file?
if ($fid_old && ($file_old = file_load(reset($fid_old)))) {
if ($fid_old && ($file_old = file_load($fid_old))) {
\Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field->uuid);
}
}
......@@ -499,19 +492,11 @@ function image_field_instance_update(FieldInstanceInterface $field_instance) {
$prior_instance = $field_instance->original;
// The value of a managed_file element can be an array if the #extended
// property is set to TRUE.
$fid_new = $field_instance->settings['default_image']['fid'];
if (isset($fid_new['fids'])) {
$fid_new = $fid_new['fids'];
}
$fid_old = $prior_instance->settings['default_image']['fid'];
if (isset($fid_old['fids'])) {
$fid_old = $fid_old['fids'];
}
// If the old and new files do not match, update the default accordingly.
$file_new = $fid_new ? file_load($fid_new[0]) : FALSE;
$file_new = $fid_new ? file_load($fid_new) : FALSE;
if ($fid_new != $fid_old) {
// Save the new file, if present.
if ($file_new) {
......@@ -520,7 +505,7 @@ function image_field_instance_update(FieldInstanceInterface $field_instance) {
\Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field_instance->uuid);
}
// Delete the old file, if present.
if ($fid_old && ($file_old = file_load($fid_old[0]))) {
if ($fid_old && ($file_old = file_load($fid_old))) {
\Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_instance->uuid);
}
}
......@@ -543,8 +528,8 @@ function image_field_entity_delete(FieldInterface $field) {
}
// The value of a managed_file element can be an array if #extended == TRUE.
$fid = (isset($field->settings['default_image']['fid']['fids']) ? $field->settings['default_image']['fid']['fids'] : $field->settings['default_image']['fid']);
if ($fid && ($file = file_load($fid[0]))) {
$fid = $field->settings['default_image']['fid'];
if ($fid && ($file = file_load($fid))) {
\Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid);
}
}
......@@ -559,12 +544,8 @@ function image_field_instance_delete(FieldInstanceInterface $field_instance) {
return;
}
// The value of a managed_file element can be an array if the #extended
// property is set to TRUE.
// The value of a managed_file element can be an array if #extended == TRUE.
$fid = $field_instance->settings['default_image']['fid'];
if (is_array($fid)) {
$fid = $fid['fid'];
}
// Remove the default image when the instance is deleted.
if ($fid && ($file = file_load($fid))) {
......
......@@ -335,6 +335,7 @@ protected function defaultImageForm(array &$element, array $settings) {
'#description' => t('Image to be shown if no image is uploaded.'),
'#default_value' => empty($settings['default_image']['fid']) ? array() : array($settings['default_image']['fid']),
'#upload_location' => $settings['uri_scheme'] . '://default_images/',
'#element_validate' => array('file_managed_file_validate', array(get_class($this), 'validateDefaultImageForm')),
);
$element['default_image']['alt'] = array(
'#type' => 'textfield',
......@@ -360,6 +361,30 @@ protected function defaultImageForm(array &$element, array $settings) {
);
}
/**
* Validates the managed_file element for the default Image form.
*
* This function ensures the fid is a scalar value and not an array. It is
* assigned as a #element_validate callback in
* \Drupal\image\Plugin\Field\FieldType\ImageItem::defaultImageForm().
*
* @param array $element
* The form element to process.
* @param array $form_state
* The form state.
*/
public static function validateDefaultImageForm(array &$element, array &$form_state) {
// Consolidate the array value of this field to a single FID as #extended
// for default image is not TRUE and this is a single value.
if (isset($element['fids']['#value'][0])) {
$value = $element['fids']['#value'][0];
}
else {
$value = 0;
}
\Drupal::formBuilder()->setValue($element, $value, $form_state);
}
/**
* {@inheritdoc}
*/
......
......@@ -33,6 +33,14 @@ public static function getInfo() {
public function testDefaultImages() {
// Create files to use as the default images.
$files = $this->drupalGetTestFiles('image');
// Create 10 files so the default image fids are not a single value.
for ($i = 1; $i <= 10; $i++) {
$filename = $this->randomName() . "$i";
$desired_filepath = 'public://' . $filename;
file_unmanaged_copy($files[0]->uri, $desired_filepath, FILE_EXISTS_ERROR);
$file = entity_create('file', array('uri' => $desired_filepath, 'filename' => $filename, 'name' => $filename));
$file->save();
}
$default_images = array();
foreach (array('field', 'instance', 'instance2', 'field_new', 'instance_new') as $image_target) {
$file = entity_create('file', (array) array_pop($files));
......@@ -173,7 +181,7 @@ public function testDefaultImages() {
);
// Upload a new default for the field.
$field->settings['default_image']['fid'] = array($default_images['field_new']->id());
$field->settings['default_image']['fid'] = $default_images['field_new']->id();
$field->save();
// Confirm that the new default is used on the article field settings form.
......
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