Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663

This commit is contained in:
Greg Anderson 2015-10-08 11:40:12 -07:00
parent eb34d130a8
commit f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions

View file

@ -112,6 +112,7 @@ class ResponsiveImageStyle extends ConfigEntityBase implements ResponsiveImageSt
'breakpoint_id' => $breakpoint_id,
'multiplier' => $multiplier,
) + $image_style_mapping;
$this->keyedImageStyleMappings = NULL;
return $this;
}
}
@ -211,7 +212,7 @@ class ResponsiveImageStyle extends ConfigEntityBase implements ResponsiveImageSt
array_walk($styles, function ($style) {
$this->addDependency('config', $style->getConfigDependencyName());
});
return $this->dependencies;
return $this;
}
/**

View file

@ -16,6 +16,8 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
/**
* Plugin for responsive image formatter.
@ -42,6 +44,20 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
*/
protected $imageStyleStorage;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The link generator.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface
*/
protected $linkGenerator;
/**
* Constructs a ResponsiveImageFormatter object.
*
@ -63,12 +79,18 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
* The responsive image style storage.
* @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
* The image style storage.
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
* The link generator service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage, EntityStorageInterface $image_style_storage) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage, EntityStorageInterface $image_style_storage, LinkGeneratorInterface $link_generator, AccountInterface $current_user) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->responsiveImageStyleStorage = $responsive_image_style_storage;
$this->imageStyleStorage = $image_style_storage;
$this->linkGenerator = $link_generator;
$this->currentUser = $current_user;
}
/**
@ -84,7 +106,9 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('entity.manager')->getStorage('responsive_image_style'),
$container->get('entity.manager')->getStorage('image_style')
$container->get('entity.manager')->getStorage('image_style'),
$container->get('link_generator'),
$container->get('current_user')
);
}
@ -118,6 +142,10 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
'#default_value' => $this->getSetting('responsive_image_style'),
'#required' => TRUE,
'#options' => $responsive_image_options,
'#description' => array(
'#markup' => $this->linkGenerator->generate($this->t('Configure Responsive Image Styles'), new Url('entity.responsive_image_style.collection')),
'#access' => $this->currentUser->hasPermission('administer responsive image styles'),
),
);
$link_types = array(
@ -164,9 +192,9 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$files = $this->getEntitiesToView($items);
$files = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
if (empty($files)) {

View file

@ -84,6 +84,7 @@ class ResponsiveImageStyleForm extends EntityForm {
);
$image_styles = image_style_options(TRUE);
$image_styles[RESPONSIVE_IMAGE_ORIGINAL_IMAGE] = $this->t('- None (original image) -');
$image_styles[RESPONSIVE_IMAGE_EMPTY_IMAGE] = $this->t('- empty image -');
if ((bool) $responsive_image_style->id() && $this->operation != 'duplicate') {
@ -100,6 +101,17 @@ class ResponsiveImageStyleForm extends EntityForm {
'#options' => $this->breakpointManager->getGroups(),
'#required' => TRUE,
'#description' => $description,
'#ajax' => array(
'callback' => '::breakpointMappingFormAjax',
'wrapper' => 'responsive-image-style-breakpoints-wrapper',
),
);
$form['keyed_styles'] = array(
'#type' => 'container',
'#attributes' => array(
'id' => 'responsive-image-style-breakpoints-wrapper',
),
);
// By default, breakpoints are ordered from smallest weight to largest:
@ -118,7 +130,7 @@ class ResponsiveImageStyleForm extends EntityForm {
);
$image_style_mapping = $responsive_image_style->getImageStyleMapping($breakpoint_id, $multiplier);
if (\Drupal::moduleHandler()->moduleExists('help')) {
$description = $this->t('See the <a href="!responsive_image_help">Responsive Image help page</a> for information on the sizes attribute.', array('!responsive_image_help' => (\Drupal::url('help.page', array('name' => 'responsive_image')))));
$description = $this->t('See the <a href=":responsive_image_help">Responsive Image help page</a> for information on the sizes attribute.', array(':responsive_image_help' => \Drupal::url('help.page', array('name' => 'responsive_image'))));
}
else {
$description = $this->t('Enable the Help module for more information on the sizes attribute.');
@ -149,12 +161,15 @@ class ResponsiveImageStyleForm extends EntityForm {
$form['keyed_styles'][$breakpoint_id][$multiplier]['sizes'] = array(
'#type' => 'textfield',
'#title' => $this->t('Sizes'),
'#default_value' => isset($image_style_mapping['image_mapping']['sizes']) ? $image_style_mapping['image_mapping']['sizes'] : '',
'#default_value' => isset($image_style_mapping['image_mapping']['sizes']) ? $image_style_mapping['image_mapping']['sizes'] : '100vw',
'#description' => $this->t('Enter the value for the sizes attribute: for example "(min-width:700px) 700px, 100vw)".'),
'#states' => array(
'visible' => array(
':input[name="keyed_styles[' . $breakpoint_id . '][' . $multiplier . '][image_mapping_type]"]' => array('value' => 'sizes'),
),
'required' => array(
':input[name="keyed_styles[' . $breakpoint_id . '][' . $multiplier . '][image_mapping_type]"]' => array('value' => 'sizes'),
),
),
);
$form['keyed_styles'][$breakpoint_id][$multiplier]['sizes_image_styles'] = array(
@ -167,6 +182,9 @@ class ResponsiveImageStyleForm extends EntityForm {
'visible' => array(
':input[name="keyed_styles[' . $breakpoint_id . '][' . $multiplier . '][image_mapping_type]"]' => array('value' => 'sizes'),
),
'required' => array(
':input[name="keyed_styles[' . $breakpoint_id . '][' . $multiplier . '][image_mapping_type]"]' => array('value' => 'sizes'),
),
),
);
@ -191,6 +209,13 @@ class ResponsiveImageStyleForm extends EntityForm {
return parent::form($form, $form_state, $responsive_image_style);
}
/**
* Get the form for mapping breakpoints to image styles.
*/
public function breakpointMappingFormAjax($form, FormStateInterface $form_state) {
return $form['keyed_styles'];
}
/**
* {@inheritdoc}
*/
@ -203,6 +228,20 @@ class ResponsiveImageStyleForm extends EntityForm {
// Remove the image style mappings since the breakpoint ID has changed.
$form_state->unsetValue('keyed_styles');
}
// Check that at least 1 image style has been selected when using sizes.
foreach ($form_state->getValue('keyed_styles') as $breakpoint_id => $multipliers) {
foreach ($multipliers as $multiplier => $image_style_mapping) {
if ($image_style_mapping['image_mapping_type'] === 'sizes') {
if (empty($image_style_mapping['sizes'])) {
$form_state->setError($form['keyed_styles'][$breakpoint_id][$multiplier]['sizes'], 'Provide a value for the sizes attribute.');
}
if (empty(array_keys(array_filter($image_style_mapping['sizes_image_styles'])))) {
$form_state->setError($form['keyed_styles'][$breakpoint_id][$multiplier]['sizes_image_styles'], 'Select at least one image style.');
}
}
}
}
}
}

View file

@ -77,7 +77,10 @@ class ResponsiveImageAdminUITest extends WebTestBase {
array('wide', '1x'),
array('wide', '2x'),
);
$image_styles = array_merge(
[RESPONSIVE_IMAGE_EMPTY_IMAGE, RESPONSIVE_IMAGE_ORIGINAL_IMAGE],
array_keys(image_style_options(FALSE))
);
foreach ($cases as $case) {
// Check if the radio buttons are present.
$this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_mapping_type]', '');
@ -85,8 +88,17 @@ class ResponsiveImageAdminUITest extends WebTestBase {
$this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_style]', '');
// Check if the sizes textfields are present.
$this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][sizes]', '');
// Check if the image styles checkboxes are present.
foreach (array_keys(image_style_options(FALSE)) as $image_style_name) {
foreach ($image_styles as $image_style_name) {
// Check if the image styles are available in the dropdowns.
$this->assertTrue($this->xpath(
'//select[@name=:name]//option[@value=:style]',
[
':name' => 'keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_style]',
':style' => $image_style_name,
]
));
// Check if the image styles checkboxes are present.
$this->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][sizes_image_styles][' . $image_style_name . ']');
}
}

View file

@ -148,6 +148,11 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
'image_mapping_type' => 'image_style',
'image_mapping' => 'large',
))
// Test the output of the original image.
->addImageStyleMapping('responsive_image_test_module.wide', '3x', array(
'image_mapping_type' => 'image_style',
'image_mapping' => RESPONSIVE_IMAGE_ORIGINAL_IMAGE,
))
->save();
}
}
@ -244,6 +249,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$this->removeWhiteSpace();
$this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.');
// Verify that the image can be downloaded.
$this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.');
@ -285,6 +291,8 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
// Assert the output of the 'srcset' attribute (small multipliers first).
$this->assertRaw('data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1x, ' . $thumbnail_style->buildUrl($image_uri) . ' 1.5x');
$this->assertRaw('/styles/medium/');
// Assert the output of the original image.
$this->assertRaw(file_create_url($image_uri) . ' 3x');
// Assert the output of the breakpoints.
$this->assertRaw('media="(min-width: 0px)"');
$this->assertRaw('media="(min-width: 560px)"');
@ -398,6 +406,52 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
$this->assertPattern('/srcset="' . preg_quote($thumbnail_style->buildUrl($image_uri), '/') . ' 1x".+?media="\(min-width: 0px\)"/');
}
/**
* Tests responsive image formatter on node display with one source.
*/
public function testResponsiveImageFieldFormattersOneSource() {
$this->responsiveImgStyle
// Test the output of an empty media query.
->addImageStyleMapping('responsive_image_test_module.empty', '1x', array(
'image_mapping_type' => 'image_style',
'image_mapping' => 'medium',
))
->addImageStyleMapping('responsive_image_test_module.empty', '2x', array(
'image_mapping_type' => 'image_style',
'image_mapping' => 'large',
))
->save();
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$field_name = Unicode::strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', array('uri_scheme' => 'public'));
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
$node_storage->resetCache(array($nid));
// Use the responsive image formatter linked to file formatter.
$display_options = array(
'type' => 'responsive_image',
'settings' => array(
'image_link' => '',
'responsive_image_style' => 'style_one',
),
);
$display = entity_get_display('node', 'article', 'default');
$display->setComponent($field_name, $display_options)
->save();
// View the node.
$this->drupalGet('node/' . $nid);
// Assert the media attribute is present if it has a value.
$large_style = ImageStyle::load('large');
$medium_style = ImageStyle::load('medium');
$node = $node_storage->load($nid);
$image_uri = File::load($node->{$field_name}->target_id)->getFileUri();
$this->assertRaw('<img srcset="' . $medium_style->buildUrl($image_uri) . ' 1x, ' . $large_style->buildUrl($image_uri) . ' 2x"');
}
/**
* Tests responsive image formatters linked to the file or node.
*
@ -451,6 +505,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
// Output should contain all image styles and all breakpoints.
$this->drupalGet('node/' . $nid);
$this->removeWhiteSpace();
switch ($link_type) {
case 'file':
// Make sure the link to the file is present.