Issue #1433796 by hampercm, claudiu.cristea, joshi.rohit100, gitesh.koli, undertext, yoroy, larowlan, rpayanm, opratr, jaffaralia, webbykat, nmudgal: Link to images styles from image field display settings
parent
d3a7d136e0
commit
3741a5e255
|
@ -8,6 +8,13 @@
|
|||
namespace Drupal\image\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Routing\UrlGeneratorInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Core\Utility\LinkGeneratorInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
|
@ -21,7 +28,77 @@ use Drupal\Core\Form\FormStateInterface;
|
|||
* }
|
||||
* )
|
||||
*/
|
||||
class ImageFormatter extends ImageFormatterBase {
|
||||
class ImageFormatter extends ImageFormatterBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The current user.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $currentUser;
|
||||
|
||||
/**
|
||||
* The link generator.
|
||||
*
|
||||
* @var \Drupal\Core\Utility\LinkGeneratorInterface
|
||||
*/
|
||||
protected $linkGenerator;
|
||||
|
||||
/**
|
||||
* The url generator service.
|
||||
*
|
||||
* @var \Drupal\Core\Routing\UrlGeneratorInterface
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
* Constructs an ImageFormatter object.
|
||||
*
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the formatter.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
|
||||
* The definition of the field to which the formatter is associated.
|
||||
* @param array $settings
|
||||
* The formatter settings.
|
||||
* @param string $label
|
||||
* The formatter label display setting.
|
||||
* @param string $view_mode
|
||||
* The view mode.
|
||||
* @param array $third_party_settings
|
||||
* Any third party settings settings.
|
||||
* @param \Drupal\Core\Session\AccountInterface $current_user
|
||||
* The current user.
|
||||
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
|
||||
* The link generator service.
|
||||
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
|
||||
* The url generator service.
|
||||
*/
|
||||
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, LinkGeneratorInterface $link_generator, UrlGeneratorInterface $url_generator) {
|
||||
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
|
||||
$this->currentUser = $current_user;
|
||||
$this->linkGenerator = $link_generator;
|
||||
$this->urlGenerator = $url_generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$configuration['field_definition'],
|
||||
$configuration['settings'],
|
||||
$configuration['label'],
|
||||
$configuration['view_mode'],
|
||||
$configuration['third_party_settings'],
|
||||
$container->get('current_user'),
|
||||
$container->get('link_generator'),
|
||||
$container->get('url_generator')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -44,8 +121,11 @@ class ImageFormatter extends ImageFormatterBase {
|
|||
'#default_value' => $this->getSetting('image_style'),
|
||||
'#empty_option' => t('None (original image)'),
|
||||
'#options' => $image_styles,
|
||||
'#description' => array(
|
||||
'#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles', array('@url' => $this->urlGenerator->generateFromRoute('image.style_list'))), new Url('image.style_list')),
|
||||
'#access' => $this->currentUser->hasPermission('administer image styles'),
|
||||
),
|
||||
);
|
||||
|
||||
$link_types = array(
|
||||
'content' => t('Content'),
|
||||
'file' => t('File'),
|
||||
|
|
|
@ -50,6 +50,27 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
|
|||
$field_name = strtolower($this->randomMachineName());
|
||||
$this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
|
||||
|
||||
// Go to manage display page.
|
||||
$this->drupalGet("admin/structure/types/manage/article/display");
|
||||
|
||||
// Test for existence of link to image styles configuration.
|
||||
$this->drupalPostAjaxForm(NULL, array(), "{$field_name}_settings_edit");
|
||||
$this->assertLinkByHref(\Drupal::url('image.style_list'), 0, 'Link to image styles configuration is found');
|
||||
|
||||
// Remove 'administer image styles' permission from testing admin user.
|
||||
$admin_user_roles = $this->admin_user->getRoles(TRUE);
|
||||
user_role_change_permissions(reset($admin_user_roles), array('administer image styles' => FALSE));
|
||||
|
||||
// Go to manage display page again.
|
||||
$this->drupalGet("admin/structure/types/manage/article/display");
|
||||
|
||||
// Test for absence of link to image styles configuration.
|
||||
$this->drupalPostAjaxForm(NULL, array(), "{$field_name}_settings_edit");
|
||||
$this->assertNoLinkByHref(\Drupal::url('image.style_list'), 'Link to image styles configuration is absent when permissions are insufficient');
|
||||
|
||||
// Restore 'administer image styles' permission to testing admin user
|
||||
user_role_change_permissions(reset($admin_user_roles), array('administer image styles' => TRUE));
|
||||
|
||||
// Create a new node with an image attached.
|
||||
$test_image = current($this->drupalGetTestFiles('image'));
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ abstract class ImageFieldTestBase extends WebTestBase {
|
|||
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
|
||||
}
|
||||
|
||||
$this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer node fields', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles'));
|
||||
$this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer node fields', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles', 'administer node display'));
|
||||
$this->drupalLogin($this->admin_user);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,3 +18,14 @@
|
|||
#field-display-overview .field-plugin-settings-edit-form .form-submit {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
#field-display-overview .form-item-fields-field-image-settings-edit-form-settings-image-style {
|
||||
white-space: normal;
|
||||
}
|
||||
#field-display-overview .form-item-fields-field-image-settings-edit-form-settings-image-style .description {
|
||||
display: inline-block;
|
||||
margin-left: 1em; /* LTR */
|
||||
}
|
||||
[dir="rtl"] #field-display-overview .form-item-fields-field-image-settings-edit-form-settings-image-style .description {
|
||||
margin-left: 0;
|
||||
margin-right: 1em;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue