Issue #2226241 by voidberg, claudiu.cristea: Saving image field or instance with an inexistent default_image crashes.
parent
7f00c591b0
commit
f61b049483
|
@ -376,9 +376,11 @@ function image_entity_presave(EntityInterface $entity) {
|
||||||
$entity_type_id = $entity->getEntityTypeId();
|
$entity_type_id = $entity->getEntityTypeId();
|
||||||
if ($entity_type_id == 'field_instance_config') {
|
if ($entity_type_id == 'field_instance_config') {
|
||||||
$field = $entity->getField();
|
$field = $entity->getField();
|
||||||
|
$default_settings = \Drupal::service('plugin.manager.field.field_type')->getDefaultInstanceSettings('image');
|
||||||
}
|
}
|
||||||
elseif ($entity_type_id == 'field_config') {
|
elseif ($entity_type_id == 'field_config') {
|
||||||
$field = $entity;
|
$field = $entity;
|
||||||
|
$default_settings = \Drupal::service('plugin.manager.field.field_type')->getDefaultSettings('image');
|
||||||
}
|
}
|
||||||
// Exit, if not saving an image field or image field instance entity.
|
// Exit, if not saving an image field or image field instance entity.
|
||||||
if (!$field || $field->type != 'image') {
|
if (!$field || $field->type != 'image') {
|
||||||
|
@ -389,20 +391,19 @@ function image_entity_presave(EntityInterface $entity) {
|
||||||
if ($fid) {
|
if ($fid) {
|
||||||
$original_fid = isset($entity->original) ? $entity->original->settings['default_image']['fid'] : NULL;
|
$original_fid = isset($entity->original) ? $entity->original->settings['default_image']['fid'] : NULL;
|
||||||
if ($fid != $original_fid) {
|
if ($fid != $original_fid) {
|
||||||
$image = \Drupal::service('image.factory')->get(file_load($fid)->getFileUri());
|
$file = file_load($fid);
|
||||||
$entity->settings['default_image']['width'] = $image->getWidth();
|
if ($file) {
|
||||||
$entity->settings['default_image']['height'] = $image->getHeight();
|
$image = \Drupal::service('image.factory')->get($file->getFileUri());
|
||||||
|
$entity->settings['default_image']['width'] = $image->getWidth();
|
||||||
|
$entity->settings['default_image']['height'] = $image->getHeight();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$entity->settings['default_image']['fid'] = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
$entity->settings['default_image'] = array(
|
$entity->settings['default_image'] += $default_settings['default_image'];
|
||||||
'fid' => NULL,
|
|
||||||
'alt' => '',
|
|
||||||
'title' => '',
|
|
||||||
'width' => NULL,
|
|
||||||
'height' => NULL,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -71,6 +71,7 @@ class ImageItem extends FileItem {
|
||||||
'default_image' => array(
|
'default_image' => array(
|
||||||
'fid' => NULL,
|
'fid' => NULL,
|
||||||
'alt' => '',
|
'alt' => '',
|
||||||
|
'title' => '',
|
||||||
'width' => NULL,
|
'width' => NULL,
|
||||||
'height' => NULL,
|
'height' => NULL,
|
||||||
),
|
),
|
||||||
|
|
|
@ -291,4 +291,43 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests image field and instance having an invalid default image.
|
||||||
|
*/
|
||||||
|
public function testInvalidDefaultImage() {
|
||||||
|
$field = array(
|
||||||
|
'name' => drupal_strtolower($this->randomName()),
|
||||||
|
'entity_type' => 'node',
|
||||||
|
'type' => 'image',
|
||||||
|
'settings' => array(
|
||||||
|
'default_image' => array(
|
||||||
|
'fid' => 100000,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$instance = array(
|
||||||
|
'field_name' => $field['name'],
|
||||||
|
'label' => $this->randomName(),
|
||||||
|
'entity_type' => 'node',
|
||||||
|
'bundle' => 'page',
|
||||||
|
'settings' => array(
|
||||||
|
'default_image' => array(
|
||||||
|
'fid' => 100000,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$field_config = entity_create('field_config', $field);
|
||||||
|
$field_config->save();
|
||||||
|
$settings = $field_config->getSettings();
|
||||||
|
// The non-existent default image should not be saved.
|
||||||
|
$this->assertNull($settings['default_image']['fid']);
|
||||||
|
|
||||||
|
$field_instance_config = entity_create('field_instance_config', $instance);
|
||||||
|
$field_instance_config->save();
|
||||||
|
$settings = $field_instance_config->getSettings();
|
||||||
|
// The non-existent default image should not be saved.
|
||||||
|
$this->assertNull($settings['default_image']['fid']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,6 @@ class UserCreateTest extends WebTestBase {
|
||||||
'indexes' => array('target_id' => array('target_id')),
|
'indexes' => array('target_id' => array('target_id')),
|
||||||
'settings' => array(
|
'settings' => array(
|
||||||
'uri_scheme' => 'public',
|
'uri_scheme' => 'public',
|
||||||
'default_image' => FALSE,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
entity_create('field_config', $field)->save();
|
entity_create('field_config', $field)->save();
|
||||||
|
@ -69,7 +68,6 @@ class UserCreateTest extends WebTestBase {
|
||||||
'title_field' => 0,
|
'title_field' => 0,
|
||||||
'max_resolution' => '85x85',
|
'max_resolution' => '85x85',
|
||||||
'min_resolution' => '',
|
'min_resolution' => '',
|
||||||
'default_image' => 0,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
entity_create('field_instance_config', $instance)->save();
|
entity_create('field_instance_config', $instance)->save();
|
||||||
|
|
Loading…
Reference in New Issue