class SiteSettingsRenderer in Site Settings and Labels 8
Class SiteSettingsRenderer.
@package Drupal\site_settings
Hierarchy
- class \Drupal\site_settings\SiteSettingsRenderer uses StringTranslationTrait
Expanded class hierarchy of SiteSettingsRenderer
1 file declares its use of SiteSettingsRenderer
- SimpleSiteSettingsBlock.php in src/
Plugin/ Block/ SimpleSiteSettingsBlock.php
1 string reference to 'SiteSettingsRenderer'
1 service uses SiteSettingsRenderer
File
- src/
SiteSettingsRenderer.php, line 15
Namespace
Drupal\site_settingsView source
class SiteSettingsRenderer {
use StringTranslationTrait;
/**
* Drupal\Core\Render\RendererInterface definition.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Default image width.
*
* @var int
*/
protected $defaultImageWidth = 25;
/**
* Default image height.
*
* @var int
*/
protected $defaultImageHeight = 25;
/**
* Constructor.
*/
public function __construct(RendererInterface $renderer, EntityTypeManagerInterface $entityTypeManager) {
$this->renderer = $renderer;
$this->entityTypeManager = $entityTypeManager;
}
/**
* Set default image size output.
*
* @param int $width
* The max image width in pixels.
* @param int $height
* The max image height in pixels.
*/
public function setDefaultImageSizeOutput($width, $height) {
$this->defaultImageWidth = $width;
$this->defaultImageHeight = $height;
}
/**
* Render the value of the added fields.
*
* @param object $field
* The field to render.
*
* @return \Drupal\Component\Render\MarkupInterface|string
* The rendered html markup.
*
* @throws \Exception
*/
public function renderField($field) {
// Get information about the field.
$definition = $field
->getFieldDefinition();
$field_type = $definition
->getType();
// Depending on the type of field, decide how to render.
switch ($field_type) {
case 'image':
return $this
->renderImage($field);
default:
return $this
->renderDefault($field, $field_type);
}
}
/**
* Render a small version of the image.
*
* @param object $field
* The field to render.
*
* @return string
* The rendered html markup.
*
* @throws \Exception
*/
protected function renderImage($field) {
if (is_object($field) && isset($field->entity)) {
$build = [
'#theme' => 'image_style',
'#width' => $this->defaultImageWidth,
'#height' => $this->defaultImageHeight,
'#style_name' => 'thumbnail',
'#uri' => $field->entity
->getFileUri(),
];
}
else {
$build['#plain_text'] = $this
->t('(none)');
}
return $this->renderer
->render($build);
}
/**
* Render a normal text value.
*
* @param \Drupal\Core\Field\FieldItemListInterface $field
* The field to render.
* @param object $field_type
* The field type to render.
*
* @return \Drupal\Component\Render\MarkupInterface
* The rendered html markup.
*
* @throws \Exception
*/
protected function renderDefault(FieldItemListInterface $field, $field_type) {
$view_builder = $this->entityTypeManager
->getViewBuilder('site_setting_entity');
$build = $view_builder
->viewField($field, [
'type' => $field_type,
'label' => 'hidden',
]);
return $this->renderer
->render($build);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SiteSettingsRenderer:: |
protected | property | Default image height. | |
SiteSettingsRenderer:: |
protected | property | Default image width. | |
SiteSettingsRenderer:: |
protected | property | Drupal\Core\Entity\EntityTypeManagerInterface definition. | |
SiteSettingsRenderer:: |
protected | property | Drupal\Core\Render\RendererInterface definition. | |
SiteSettingsRenderer:: |
protected | function | Render a normal text value. | |
SiteSettingsRenderer:: |
public | function | Render the value of the added fields. | |
SiteSettingsRenderer:: |
protected | function | Render a small version of the image. | |
SiteSettingsRenderer:: |
public | function | Set default image size output. | |
SiteSettingsRenderer:: |
public | function | Constructor. | |
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. |