You are here

class SiteSettingsRenderer in Site Settings and Labels 8

Class SiteSettingsRenderer.

@package Drupal\site_settings

Hierarchy

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'
site_settings.services.yml in ./site_settings.services.yml
site_settings.services.yml
1 service uses SiteSettingsRenderer
site_settings.renderer in ./site_settings.services.yml
Drupal\site_settings\SiteSettingsRenderer

File

src/SiteSettingsRenderer.php, line 15

Namespace

Drupal\site_settings
View 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

Namesort descending Modifiers Type Description Overrides
SiteSettingsRenderer::$defaultImageHeight protected property Default image height.
SiteSettingsRenderer::$defaultImageWidth protected property Default image width.
SiteSettingsRenderer::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManagerInterface definition.
SiteSettingsRenderer::$renderer protected property Drupal\Core\Render\RendererInterface definition.
SiteSettingsRenderer::renderDefault protected function Render a normal text value.
SiteSettingsRenderer::renderField public function Render the value of the added fields.
SiteSettingsRenderer::renderImage protected function Render a small version of the image.
SiteSettingsRenderer::setDefaultImageSizeOutput public function Set default image size output.
SiteSettingsRenderer::__construct public function Constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.