You are here

class CarouselService in bootstrap simple carousel 8

CarouselService Class.

Provides functions for the module.

@category Class @package Drupal\bootstrap_simple_carousel\Service

Hierarchy

Expanded class hierarchy of CarouselService

3 files declare their use of CarouselService
CarouselServiceTest.php in tests/src/Unit/Service/CarouselServiceTest.php
EditForm.php in src/Form/EditForm.php
ItemsForm.php in src/Form/ItemsForm.php
1 string reference to 'CarouselService'
bootstrap_simple_carousel.services.yml in ./bootstrap_simple_carousel.services.yml
bootstrap_simple_carousel.services.yml
1 service uses CarouselService
bootstrap_simple_carousel.carousel_service in ./bootstrap_simple_carousel.services.yml
Drupal\bootstrap_simple_carousel\Service\CarouselService

File

src/Service/CarouselService.php, line 17

Namespace

Drupal\bootstrap_simple_carousel\Service
View source
class CarouselService extends ServiceProviderBase {
  use \Drupal\Core\StringTranslation\StringTranslationTrait;

  /**
   * This will hold Renderer object.
   *
   * @var \Drupal\Core\Render\Renderer
   */
  protected $renderer;

  /**
   * This will hold File object.
   *
   * @var \Drupal\file\FileInterface
   */
  protected $file;

  /**
   * CarouselService constructor.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity manager interface.
   */
  public function __construct(RendererInterface $renderer, EntityTypeManagerInterface $entityTypeManager) {
    $this->renderer = $renderer;
    $this->file = $entityTypeManager
      ->getStorage('file');
  }

  /**
   * Return a rendered image.
   *
   * @param int $image_id
   *   The image id for the carousel.
   * @param string $image_style
   *   The image style for the carousel.
   * @param array $params
   *   An array of parameters.
   *
   * @throws \Exception
   *
   * @return string
   *   Rendered image
   */
  public function renderImageById($image_id, $image_style = 'thumbnail', array $params = []) {
    $image = '';
    $imageFile = $this->file
      ->load($image_id);
    if (!empty($imageFile)) {
      $imageTheme = [
        '#theme' => 'image_style',
        '#style_name' => $image_style,
        '#uri' => $imageFile
          ->getFileUri(),
        '#alt' => $params['alt'] ?? '',
        '#title' => $params['title'] ?? '',
      ];
      $image = $this->renderer
        ->render($imageTheme);
    }
    return $image;
  }

  /**
   * Return a Render Link.
   *
   * @param string $url
   *   The url for the render link.
   * @param string $title
   *   The title for the render link.
   * @param array $attributes
   *   The array of attributes.
   *
   * @throws \Exception
   *
   * @return string
   *   Rendered link
   */
  public function renderLink($url, $title, array $attributes = []) {
    $linkTheme = [
      '#type' => 'link',
      '#title' => $title,
      '#url' => $url,
      '#options' => [
        'attributes' => $attributes,
        'html' => FALSE,
      ],
    ];
    $link = $this->renderer
      ->render($linkTheme);
    return $link;
  }

  /**
   * Return the statuses.
   *
   * @return array
   *   Of statuses
   */
  public function getStatuses() {
    return [
      0 => $this
        ->t('Inactive'),
      1 => $this
        ->t('Active'),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CarouselService::$file protected property This will hold File object.
CarouselService::$renderer protected property This will hold Renderer object.
CarouselService::getStatuses public function Return the statuses.
CarouselService::renderImageById public function Return a rendered image.
CarouselService::renderLink public function Return a Render Link.
CarouselService::__construct public function CarouselService constructor.
ServiceProviderBase::alter public function Modifies existing service definitions. Overrides ServiceModifierInterface::alter 5
ServiceProviderBase::register public function Registers services to the container. Overrides ServiceProviderInterface::register 1
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.