diff --git a/src/Plugin/Field/FieldFormatter/SvgImageFieldFormatter.php b/src/Plugin/Field/FieldFormatter/SvgImageFieldFormatter.php index a39d186..845b9ab 100644 --- a/src/Plugin/Field/FieldFormatter/SvgImageFieldFormatter.php +++ b/src/Plugin/Field/FieldFormatter/SvgImageFieldFormatter.php @@ -8,9 +8,10 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; use enshrined\svgSanitize\Sanitizer; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\File\FileUrlGeneratorInterface; /** * Plugin implementation of the 'svg_formatter' formatter. @@ -31,6 +32,20 @@ class SvgImageFieldFormatter extends FormatterBase implements ContainerFactoryPl */ public $logger; + /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * The file URL generator. + * + * @var \Drupal\Core\File\FileUrlGeneratorInterface + */ + protected $fileUrlGenerator; + /** * {@inheritdoc} */ @@ -217,7 +232,7 @@ class SvgImageFieldFormatter extends FormatterBase implements ContainerFactoryPl continue; } $uri = $item->entity->getFileUri(); - $stageFileProxy = \Drupal::moduleHandler()->moduleExists('stage_file_proxy'); + $stageFileProxy = $this->moduleHandler->moduleExists('stage_file_proxy'); if (file_exists($uri) === FALSE && !$stageFileProxy) { $this->logger->error('The specified file %file could not be displayed by image formatter due file not exists.', ['%file' => $uri]); continue; @@ -268,7 +283,7 @@ class SvgImageFieldFormatter extends FormatterBase implements ContainerFactoryPl // context to ensure different file URLs are generated for different // sites in a multisite setup, including HTTP and HTTPS versions of the // same site. Fix in https://www.drupal.org/node/2646744. - $url = \Drupal::service('file_url_generator')->generate($uri); + $url = $this->fileUrlGenerator->generate($uri); $cache_contexts[] = 'url.site'; } @@ -308,6 +323,10 @@ class SvgImageFieldFormatter extends FormatterBase implements ContainerFactoryPl * Any third party settings. * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger * Logger. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler service. + * @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator + * The file URL generator. */ public function __construct( $plugin_id, @@ -317,9 +336,13 @@ class SvgImageFieldFormatter extends FormatterBase implements ContainerFactoryPl $label, $view_mode, array $third_party_settings, - LoggerChannelFactoryInterface $logger + LoggerChannelFactoryInterface $logger, + ModuleHandlerInterface $module_handler, + FileUrlGeneratorInterface $file_url_generator ) { $this->logger = $logger->get('svg_image_field'); + $this->moduleHandler = $module_handler; + $this->fileUrlGenerator = $file_url_generator; parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); } @@ -341,7 +364,9 @@ class SvgImageFieldFormatter extends FormatterBase implements ContainerFactoryPl $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], - $container->get('logger.factory') + $container->get('logger.factory'), + $container->get('module_handler'), + $container->get('file_url_generator') ); } diff --git a/src/Plugin/Field/FieldType/SvgImageFieldItem.php b/src/Plugin/Field/FieldType/SvgImageFieldItem.php index cdd778d..3c6884d 100644 --- a/src/Plugin/Field/FieldType/SvgImageFieldItem.php +++ b/src/Plugin/Field/FieldType/SvgImageFieldItem.php @@ -150,15 +150,15 @@ class SvgImageFieldItem extends FileItem { $scheme_options = \Drupal::service('stream_wrapper_manager')->getNames(StreamWrapperInterface::WRITE_VISIBLE); $element['uri_scheme'] = [ '#type' => 'radios', - '#title' => t('Upload destination'), + '#title' => $this->t('Upload destination'), '#options' => $scheme_options, '#default_value' => $settings['uri_scheme'], - '#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'), + '#description' => $this->t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'), ]; // Add default_image element. static::defaultImageForm($element, $settings); - $element['default_image']['#description'] = t('If no image is uploaded, this image will be shown on display.'); + $element['default_image']['#description'] = $this->t('If no image is uploaded, this image will be shown on display.'); return $element; } @@ -177,16 +177,16 @@ class SvgImageFieldItem extends FileItem { // Add title and alt configuration options. $element['alt_field'] = [ '#type' => 'checkbox', - '#title' => t('Enable Alt field'), + '#title' => $this->t('Enable Alt field'), '#default_value' => $settings['alt_field'], - '#description' => t('The alt attribute may be used by search engines, screen readers, and when the image cannot be loaded. Enabling this field is recommended.'), + '#description' => $this->t('The alt attribute may be used by search engines, screen readers, and when the image cannot be loaded. Enabling this field is recommended.'), '#weight' => 9, ]; $element['alt_field_required'] = [ '#type' => 'checkbox', - '#title' => t('Alt field required'), + '#title' => $this->t('Alt field required'), '#default_value' => $settings['alt_field_required'], - '#description' => t('Making this field required is recommended.'), + '#description' => $this->t('Making this field required is recommended.'), '#weight' => 10, '#states' => [ 'visible' => [ @@ -196,14 +196,14 @@ class SvgImageFieldItem extends FileItem { ]; $element['title_field'] = [ '#type' => 'checkbox', - '#title' => t('Enable Title field'), + '#title' => $this->t('Enable Title field'), '#default_value' => $settings['title_field'], - '#description' => t('The title attribute is used as a tooltip when the mouse hovers over the image. Enabling this field is not recommended as it can cause problems with screen readers.'), + '#description' => $this->t('The title attribute is used as a tooltip when the mouse hovers over the image. Enabling this field is not recommended as it can cause problems with screen readers.'), '#weight' => 11, ]; $element['title_field_required'] = [ '#type' => 'checkbox', - '#title' => t('Title field required'), + '#title' => $this->t('Title field required'), '#default_value' => $settings['title_field_required'], '#weight' => 12, '#states' => [ @@ -215,7 +215,7 @@ class SvgImageFieldItem extends FileItem { // Add default_image element. static::defaultImageForm($element, $settings); - $element['default_image']['#description'] = t("If no image is uploaded, this image will be shown on display and will override the field's default image."); + $element['default_image']['#description'] = $this->t("If no image is uploaded, this image will be shown on display and will override the field's default image."); return $element; } @@ -230,7 +230,7 @@ class SvgImageFieldItem extends FileItem { protected function defaultImageForm(array &$element, array $settings) { $element['default_image'] = [ '#type' => 'details', - '#title' => t('Default image'), + '#title' => $this->t('Default image'), '#open' => TRUE, ]; // Convert the stored UUID to a FID. @@ -244,8 +244,8 @@ class SvgImageFieldItem extends FileItem { $upload_validators['svg_image_field_validate_mime_type'] = []; $element['default_image']['uuid'] = [ '#type' => 'managed_file', - '#title' => t('Image'), - '#description' => t('Image to be shown if no image is uploaded.'), + '#title' => $this->t('Image'), + '#description' => $this->t('Image to be shown if no image is uploaded.'), '#default_value' => $fids, '#upload_location' => $settings['uri_scheme'] . '://default_images/', '#element_validate' => [ @@ -256,15 +256,15 @@ class SvgImageFieldItem extends FileItem { ]; $element['default_image']['alt'] = [ '#type' => 'textfield', - '#title' => t('Alternative text'), - '#description' => t('This text will be used by screen readers, search engines, and when the image cannot be loaded.'), + '#title' => $this->t('Alternative text'), + '#description' => $this->t('This text will be used by screen readers, search engines, and when the image cannot be loaded.'), '#default_value' => $settings['default_image']['alt'], '#maxlength' => 512, ]; $element['default_image']['title'] = [ '#type' => 'textfield', - '#title' => t('Title'), - '#description' => t('The title attribute is used as a tooltip when the mouse hovers over the image.'), + '#title' => $this->t('Title'), + '#description' => $this->t('The title attribute is used as a tooltip when the mouse hovers over the image.'), '#default_value' => $settings['default_image']['title'], '#maxlength' => 1024, ]; diff --git a/src/Plugin/Field/FieldWidget/SvgImageFieldWidget.php b/src/Plugin/Field/FieldWidget/SvgImageFieldWidget.php index 1fa9d36..a875882 100644 --- a/src/Plugin/Field/FieldWidget/SvgImageFieldWidget.php +++ b/src/Plugin/Field/FieldWidget/SvgImageFieldWidget.php @@ -83,13 +83,13 @@ class SvgImageFieldWidget extends FileWidget { public function settingsForm(array $form, FormStateInterface $form_state) { $element = parent::settingsForm($form, $form_state); $element['preview_image_max_width'] = [ - '#title' => t('Preview image max width'), + '#title' => $this->t('Preview image max width'), '#type' => 'number', '#default_value' => $this->getSetting('preview_image_max_width'), '#weight' => 15, ]; $element['preview_image_max_height'] = [ - '#title' => t('Preview image max height'), + '#title' => $this->t('Preview image max height'), '#type' => 'number', '#default_value' => $this->getSetting('preview_image_max_height'), '#weight' => 16, @@ -103,8 +103,8 @@ class SvgImageFieldWidget extends FileWidget { */ public function settingsSummary() { $summary = parent::settingsSummary(); - $preview_image_max_width = t('Preview image width: @width', ['@width' => $this->getSetting('preview_image_max_width')]); - $preview_image_max_height = t('Preview image height: @height', ['@height' => $this->getSetting('preview_image_max_height')]); + $preview_image_max_width = $this->t('Preview image width: @width', ['@width' => $this->getSetting('preview_image_max_width')]); + $preview_image_max_height = $this->t('Preview image height: @height', ['@height' => $this->getSetting('preview_image_max_height')]); array_unshift($summary, $preview_image_max_width); array_unshift($summary, $preview_image_max_height);