class ConfigDefaultImageFormatter in Config default image 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/ConfigDefaultImageFormatter.php \Drupal\config_default_image\Plugin\Field\FieldFormatter\ConfigDefaultImageFormatter
Plugin implementation of the 'image' formatter.
Plugin annotation
@FieldFormatter(
id = "config_default_image",
label = @Translation("Image or default image"),
field_types = {
"image"
},
quickedit = {
"editor" = "image"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase
- class \Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase
- class \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase
- class \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter implements ContainerFactoryPluginInterface
- class \Drupal\config_default_image\Plugin\Field\FieldFormatter\ConfigDefaultImageFormatter
- class \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter implements ContainerFactoryPluginInterface
- class \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase
- class \Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ConfigDefaultImageFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ ConfigDefaultImageFormatter.php, line 31
Namespace
Drupal\config_default_image\Plugin\Field\FieldFormatterView source
class ConfigDefaultImageFormatter extends ImageFormatter {
/**
* The file system.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* 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\Entity\EntityStorageInterface $image_style_storage
* The image style storage.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage, FileSystemInterface $file_system) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage);
$this->fileSystem = $file_system;
}
/**
* {@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('entity_type.manager')
->getStorage('image_style'), $container
->get('file_system'));
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'default_image' => [
'path' => '',
'use_image_style' => FALSE,
'alt' => '',
'title' => '',
'width' => NULL,
'height' => NULL,
],
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
* @see \Drupal\image\Plugin\Field\FieldType\ImageItem
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$settings = $this
->getSettings();
$element['default_image'] = [
'#type' => 'details',
'#title' => t('Default image'),
'#open' => TRUE,
'#required' => TRUE,
];
$element['default_image']['path'] = [
'#type' => 'textfield',
'#title' => t('Image path'),
'#description' => t('Drupal path to the image to be shown if no image is uploaded (the image would typically be in a git-managed directory so that it can be deployed easily). Example: /themes/custom/my_theme/img/default_image.jpg'),
'#default_value' => $settings['default_image']['path'],
'#required' => TRUE,
];
$element['default_image']['use_image_style'] = [
'#type' => 'checkbox',
'#title' => t('Apply the image style'),
'#description' => t('Check this box to use the image style on the default image'),
'#default_value' => $settings['default_image']['use_image_style'],
];
$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.'),
'#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.'),
'#default_value' => $settings['default_image']['title'],
'#maxlength' => 1024,
];
$element['default_image']['width'] = [
'#type' => 'value',
'#value' => $settings['default_image']['width'],
];
$element['default_image']['height'] = [
'#type' => 'value',
'#value' => $settings['default_image']['height'],
];
$element['default_image']['#description'] = t('If no image is set for the field (not even a field-level default image), this image will be shown on display.');
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = t('Fallback to a default image');
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
if (empty($elements)) {
$default_image = $this
->getSetting('default_image');
$image_path = $default_image['path'];
if (!empty($image_path)) {
if ($default_image['use_image_style']) {
// $image_path must be ready for
// Drupal\image\Entity\ImageStyle::buildUri().
// This needs a valid scheme.
// As long as https://www.drupal.org/project/drupal/issues/1308152 is
// not fixed, files stored outside from public, private and temporary
// directories have no scheme.
// So that if our path has no scheme, we copy the file to the public
// files directory and add it as scheme.
if (!StreamWrapperManager::getScheme($image_path)) {
$image_path = ltrim($image_path, '/');
$destination = 'public://config_default_image/' . $image_path;
$directory = $this->fileSystem
->dirname($destination);
$this->fileSystem
->prepareDirectory($directory, $this->fileSystem::CREATE_DIRECTORY);
if (!file_exists($destination)) {
$image_path = $this->fileSystem
->copy($image_path, $destination);
}
else {
$image_path = $destination;
}
}
}
$file = File::create([
'uid' => 0,
'filename' => $this->fileSystem
->basename($image_path),
'uri' => $image_path,
'status' => 1,
]);
$url = NULL;
$image_link_setting = $this
->getSetting('image_link');
// Check if the formatter involves a link.
if ($image_link_setting == 'content') {
$entity = $items
->getEntity();
if (!$entity
->isNew()) {
$url = $entity
->toUrl();
}
}
elseif ($image_link_setting == 'file') {
$link_file = TRUE;
}
$cache_tags = [];
$cache_contexts = [];
if (isset($link_file)) {
$cache_contexts[] = 'url.site';
}
// @see ImageFormatterBase
// Clone the FieldItemList into a runtime-only object for the formatter,
// so that the fallback image can be rendered without affecting the
// field values in the entity being rendered.
$items = clone $items;
$items
->setValue([
'target_id' => $file
->id(),
'alt' => $default_image['alt'],
'title' => $default_image['title'],
'width' => $default_image['width'],
'height' => $default_image['height'],
'entity' => $file,
'_loaded' => TRUE,
'_is_default' => TRUE,
]);
$item = $items[0];
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item_attributes = $item->_attributes;
unset($item->_attributes);
$elements[] = [
'#theme' => 'image_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $default_image['use_image_style'] ? $this
->getSetting('image_style') : FALSE,
'#url' => $url,
'#cache' => [
'tags' => $cache_tags,
'contexts' => $cache_contexts,
],
];
}
}
return $elements;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigDefaultImageFormatter:: |
protected | property | The file system. | |
ConfigDefaultImageFormatter:: |
public static | function |
Creates an instance of the plugin. Overrides ImageFormatter:: |
|
ConfigDefaultImageFormatter:: |
public static | function |
Defines the default settings for this plugin. Overrides ImageFormatter:: |
|
ConfigDefaultImageFormatter:: |
public | function |
Overrides ImageFormatter:: |
|
ConfigDefaultImageFormatter:: |
public | function |
Returns a short summary for the current formatter settings. Overrides ImageFormatter:: |
|
ConfigDefaultImageFormatter:: |
public | function |
Builds a renderable array for a field value. Overrides ImageFormatter:: |
|
ConfigDefaultImageFormatter:: |
public | function |
Constructs an ImageFormatter object. Overrides ImageFormatter:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityReferenceFormatterBase:: |
public | function |
Loads the entities referenced in that field across all the entities being
viewed. Overrides FormatterBase:: |
|
EntityReferenceFormatterBase:: |
public | function |
Overrides FormatterBase:: |
|
FileFormatterBase:: |
protected | function |
Checks access to the given entity. Overrides EntityReferenceFormatterBase:: |
|
FileFormatterBase:: |
protected | function |
Returns whether the entity referenced by an item needs to be loaded. Overrides EntityReferenceFormatterBase:: |
1 |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The label display setting. | |
FormatterBase:: |
protected | property |
The formatter settings. Overrides PluginSettingsBase:: |
|
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
protected | function | Returns the value of a field setting. | |
FormatterBase:: |
protected | function | Returns the array of field settings. | |
FormatterBase:: |
public static | function |
Returns if the formatter can be used for the provided field. Overrides FormatterInterface:: |
14 |
ImageFormatter:: |
protected | property | The current user. | |
ImageFormatter:: |
protected | property | The image style entity storage. | |
ImageFormatter:: |
public | function |
Calculates dependencies for the configured plugin. Overrides PluginSettingsBase:: |
|
ImageFormatter:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsBase:: |
|
ImageFormatterBase:: |
protected | function |
Returns the referenced entities for display. Overrides EntityReferenceFormatterBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |