You are here

class ThemePreprocess in Image Lazyloader 8

Class ThemePreprocess.

Hierarchy

Expanded class hierarchy of ThemePreprocess

1 file declares its use of ThemePreprocess
ThemePreprocessTest.php in tests/src/Unit/ThemePreprocessTest.php
1 string reference to 'ThemePreprocess'
lazyloader.services.yml in ./lazyloader.services.yml
lazyloader.services.yml
1 service uses ThemePreprocess
lazyloader.preprocess in ./lazyloader.services.yml
\Drupal\lazyloader\ThemePreprocess

File

src/ThemePreprocess.php, line 10

Namespace

Drupal\lazyloader
View source
class ThemePreprocess {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Creates a new ThemePreprocess instance.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * Attaches the Lazyloader library to a render array.
   *
   * @param array $vars
   *   The render array.
   *
   * @return array
   *   The render array with added library.
   */
  public function attachLibrary(array $vars) {
    $config = $this->configFactory
      ->get('lazyloader.configuration');
    if (!$config
      ->get('enabled')) {
      return $vars;
    }
    $vars['#attached']['library'][] = $this
      ->determineLibraryToAttach();
    return $vars;
  }

  /**
   * Adds the cache tags.
   *
   * @param array $vars
   *   The variables.
   *
   * @return array
   *   The input array with cache tags.
   */
  public function addCacheTags(array $vars) {
    $vars['#cache']['tags'][] = 'config:lazyloader.configuration';
    return $vars;
  }

  /**
   * Determines the library to attach.
   *
   * @return string
   *   The library to attach.
   */
  private function determineLibraryToAttach() {
    $config = $this->configFactory
      ->get('lazyloader.configuration');
    if ($config
      ->get('debugging')) {
      $library = 'lazyloader/lazysizes';
      return $library;
    }
    if ($config
      ->get('cdn') || !file_exists('libraries/lazysizes/lazysizes.min.js')) {
      $library = 'lazyloader/lazysizes-min.cdn';
      return $library;
    }
    $library = 'lazyloader/lazysizes-min';
    return $library;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThemePreprocess::$configFactory protected property The config factory.
ThemePreprocess::addCacheTags public function Adds the cache tags.
ThemePreprocess::attachLibrary public function Attaches the Lazyloader library to a render array.
ThemePreprocess::determineLibraryToAttach private function Determines the library to attach.
ThemePreprocess::__construct public function Creates a new ThemePreprocess instance.