You are here

class Loader in Bamboo Twig 8.4

Same name and namespace in other branches
  1. 8.5 bamboo_twig_loader/src/TwigExtension/Loader.php \Drupal\bamboo_twig_loader\TwigExtension\Loader
  2. 8.2 bamboo_twig_loader/src/TwigExtension/Loader.php \Drupal\bamboo_twig_loader\TwigExtension\Loader
  3. 8.3 bamboo_twig_loader/src/TwigExtension/Loader.php \Drupal\bamboo_twig_loader\TwigExtension\Loader

Provides some loaders as Twig Extensions.

Hierarchy

  • class \Drupal\bamboo_twig\TwigExtension\TwigExtensionBase extends \Drupal\bamboo_twig\TwigExtension\Twig_Extension uses \Symfony\Component\DependencyInjection\ContainerAwareTrait
    • class \Drupal\bamboo_twig_loader\TwigExtension\Loader

Expanded class hierarchy of Loader

1 string reference to 'Loader'
bamboo_twig_loader.services.yml in bamboo_twig_loader/bamboo_twig_loader.services.yml
bamboo_twig_loader/bamboo_twig_loader.services.yml
1 service uses Loader
bamboo_twig_loader.twig.loader in bamboo_twig_loader/bamboo_twig_loader.services.yml
Drupal\bamboo_twig_loader\TwigExtension\Loader

File

bamboo_twig_loader/src/TwigExtension/Loader.php, line 10

Namespace

Drupal\bamboo_twig_loader\TwigExtension
View source
class Loader extends TwigExtensionBase {

  /**
   * {@inheritdoc}
   */
  public function getFunctions() {
    return [
      new \Twig_SimpleFunction('bamboo_load_entity', [
        $this,
        'loadEntity',
      ]),
      new \Twig_SimpleFunction('bamboo_load_field', [
        $this,
        'loadField',
      ]),
      new \Twig_SimpleFunction('bamboo_load_currentuser', [
        $this,
        'loadCurrentUser',
      ]),
      new \Twig_SimpleFunction('bamboo_load_image', [
        $this,
        'loadImage',
      ]),
    ];
  }

  /**
   * Unique identifier for this Twig extension.
   */
  public function getName() {
    return 'bamboo_twig_loader.twig.loader';
  }

  /**
   * Returns an entity object in the current context language.
   *
   * Keep in mind languages loading priorities:
   *  1. Get the entity in the current page lang,
   *  2. When not found, try to fetch the entity in the default site lang,
   *  3. When not found in 2 previous attempts, fetch the original entity lang.
   *
   * @param string $entity_type
   *   The entity type.
   * @param mixed $id
   *   (optional) The ID of the entity to load.
   * @param string $langcode
   *   (optional) For which language the entity should be rendered, defaults to
   *   the current content language.
   *
   * @return null|Drupal\Core\Entity\EntityInterface
   *   An entity object for the entity or NULL if the entity does not exist.
   */
  public function loadEntity($entity_type, $id = NULL, $langcode = NULL) {
    $entityRepository = $this
      ->getEntityRepository();
    $entity = $id ? $this
      ->getEntityTypeManager()
      ->getStorage($entity_type)
      ->load($id) : $this
      ->getCurrentRouteMatch()
      ->getParameter($entity_type);
    if (!$entity) {
      return NULL;
    }

    // Get the entity in the current context language.
    return $entityRepository
      ->getTranslationFromContext($entity, $langcode);
  }

  /**
   * Returns the field object for a single entity field.
   *
   * @param string $field_name
   *   The field name.
   * @param string $entity_type
   *   The entity type.
   * @param mixed $id
   *   (optional) The ID of the entity to render.
   * @param string $langcode
   *   (optional) Language code to load translation.
   *
   * @return null|Drupal\Core\Field\FieldItemListInterface
   *   A field object for the entity or NULL if the value does not exist.
   */
  public function loadField($field_name, $entity_type, $id = NULL, $langcode = NULL) {

    // Load the entity view using the current content language.
    if (!$langcode) {
      $langcode = $this
        ->getLanguageManager()
        ->getCurrentLanguage()
        ->getId();
    }
    $entity = $this
      ->loadEntity($entity_type, $id, $langcode);
    if (!$entity) {
      return NULL;
    }

    // Ensure the entity has the requested field.
    if (!$entity
      ->hasField($field_name)) {
      return NULL;
    }

    // Do not continue if the field is empty.
    if ($entity
      ->get($field_name)
      ->isEmpty()) {
      return NULL;
    }
    if (isset($entity->{$field_name})) {
      return $entity->{$field_name};
    }
    return NULL;
  }

  /**
   * Return the current user object.
   *
   * @return \Drupal\user\Entity\User|null
   *   The current user object or NULL when anonymous.
   */
  public function loadCurrentUser() {
    $currentUser = $this
      ->getCurrentUser();
    if ($currentUser
      ->isAnonymous()) {
      return NULL;
    }
    return $this
      ->getUserStorage()
      ->load($currentUser
      ->id());
  }

  /**
   * Load a ImageInterface object for an original image path or URI.
   *
   * @param string $path
   *   The path or URI to the original image.
   *
   * @return \Drupal\Core\Image\ImageInterface
   *   An Image object.
   */
  public function loadImage($path) {
    return $this
      ->getImageFactory()
      ->get($path);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Loader::getFunctions public function
Loader::getName public function Unique identifier for this Twig extension. Overrides TwigExtensionBase::getName
Loader::loadCurrentUser public function Return the current user object.
Loader::loadEntity public function Returns an entity object in the current context language.
Loader::loadField public function Returns the field object for a single entity field.
Loader::loadImage public function Load a ImageInterface object for an original image path or URI.
TwigExtensionBase::getBlockStorage protected function Return the block storage.
TwigExtensionBase::getConfigFactory protected function Provides an interface for a configuration object factory.
TwigExtensionBase::getCurrentRouteMatch protected function Return the current route match.
TwigExtensionBase::getCurrentUser protected function Lazy loading for the Drupal current user account proxy.
TwigExtensionBase::getDateFormatter protected function Provides a service to handle various date related functionality.
TwigExtensionBase::getEntityRepository protected function Lazy loading for the Drupal entity repository.
TwigExtensionBase::getEntityTypeManager protected function Lazy loading for the Drupal entity type manager.
TwigExtensionBase::getExtensionGuesser protected function Return a singleton mime type to file extension guesser.
TwigExtensionBase::getFieldTypeManager protected function Return the factory for image objects.
TwigExtensionBase::getFileStorage protected function Return the file storage.
TwigExtensionBase::getFileSystemObject protected function Provides helpers to operate on files and stream wrappers.
TwigExtensionBase::getFormBuilder protected function Provides an interface for form building and processing.
TwigExtensionBase::getImageFactory protected function Return the factory for image objects.
TwigExtensionBase::getImageStyleStorage protected function Provides an interface defining an image style.
TwigExtensionBase::getLanguageManager protected function Returns the language manager service.
TwigExtensionBase::getMenuLinkTree protected function Interface for loading, transforming and rendering menu link trees.
TwigExtensionBase::getPluginManagerBlock protected function Manages discovery and instantiation of block plugins.
TwigExtensionBase::getSettingsSingleton protected function Read only settings singleton.
TwigExtensionBase::getStateFactory protected function The state storage service.
TwigExtensionBase::getToken protected function Return the token service.
TwigExtensionBase::getUserStorage protected function Return the user storage.