Commit f070288c authored by alexpott's avatar alexpott
Browse files

Issue #2143653 by swentel, tstoeckler, yoroy | ifrik: Added Show default image on image field.

parent 073c00bb
......@@ -10,6 +10,7 @@
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Drupal\file\Plugin\Field\FieldWidget\FileWidget;
/**
......@@ -135,6 +136,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$element['#alt_field'] = $field_settings['alt_field'];
$element['#alt_field_required'] = $field_settings['alt_field_required'];
// Default image.
$default_image = $field_settings['default_image'];
if (empty($default_image['fid'])) {
$default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
}
$element['#default_image'] = !empty($default_image['fid']) ? $default_image : array();
return $element;
}
......@@ -177,6 +185,7 @@ public static function process($element, FormStateInterface $form_state, $form)
}
$element['preview'] = array(
'#weight' => -10,
'#theme' => 'image_style',
'#width' => $variables['width'],
'#height' => $variables['height'],
......@@ -195,6 +204,20 @@ public static function process($element, FormStateInterface $form_state, $form)
'#value' => $variables['height'],
);
}
elseif (!empty($element['#default_image'])) {
$default_image = $element['#default_image'];
$file = File::load($default_image['fid']);
if (!empty($file)) {
$element['preview'] = array(
'#weight' => -10,
'#theme' => 'image_style',
'#width' => $default_image['width'],
'#height' => $default_image['height'],
'#style_name' => $element['#preview_image_style'],
'#uri' => $file->getFileUri(),
);
}
}
// Add the additional alt and title fields.
$element['alt'] = array(
......@@ -204,7 +227,7 @@ public static function process($element, FormStateInterface $form_state, $form)
'#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
// @see http://www.gawds.org/show.php?contentid=28
'#maxlength' => 512,
'#weight' => -2,
'#weight' => -12,
'#access' => (bool) $item['fids'] && $element['#alt_field'],
'#element_validate' => $element['#alt_field_required'] == 1 ? array(array(get_called_class(), 'validateRequiredFields')) : array(),
);
......@@ -214,7 +237,7 @@ public static function process($element, FormStateInterface $form_state, $form)
'#default_value' => isset($item['title']) ? $item['title'] : '',
'#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
'#maxlength' => 1024,
'#weight' => -1,
'#weight' => -11,
'#access' => (bool) $item['fids'] && $element['#title_field'],
'#element_validate' => $element['#title_field_required'] == 1 ? array(array(get_called_class(), 'validateRequiredFields')) : array(),
);
......
......@@ -6,6 +6,7 @@
*/
namespace Drupal\image\Tests;
use Drupal\file\Entity\File;
/**
* Tests setting up default images both to the field and field instance.
......@@ -245,6 +246,11 @@ public function testDefaultImages() {
)
);
// Confirm the default image is shown on the node form.
$file = File::load($default_images['instance_new']->id());
$this->drupalGet('node/add/article');
$this->assertRaw($file->getFilename());
// Remove the instance default from articles.
$instance->settings['default_image']['fid'] = 0;
$instance->save();
......@@ -282,6 +288,11 @@ public function testDefaultImages() {
$non_image = $this->drupalGetTestFiles('text');
$this->drupalPostForm(NULL, array('files[instance_settings_default_image_fid]' => drupal_realpath($non_image[0]->uri)), t("Upload"));
$this->assertText(t('The specified file text-0.txt could not be uploaded. Only files with the following extensions are allowed: png gif jpg jpeg.'), 'Non-image file cannot be used as default image.');
// Confirm the default image is shown on the node form.
$file = File::load($default_images['field_new']->id());
$this->drupalGet('node/add/article');
$this->assertRaw($file->getFilename());
}
/**
......
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