Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/dds_global_content / src / GlobalContentViewBuilder.php
Size: Mime:
<?php
namespace Drupal\dds_global_content;

use Drupal\Core\Language\LanguageInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class GlobalContentViewBuilder {

  private $manager;

  private $storage;

  private $requestStack;

  public function __construct(GlobalContentManager $manager, GlobalContentStorage $storage, RequestStack $requestStack) {
    $this->manager = $manager;
    $this->storage = $storage;
    $this->requestStack = $requestStack;
  }

  /**
   * @param string $machine_name
   * @param string|null $langcode
   *
   * @return ViewableGlobalContent
   */
  public function get($machine_name, $langcode = NULL) {
    $views = &drupal_static(__FUNCTION__, []);

    $langcode = (empty($langcode)) ? \Drupal::languageManager()
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
      ->getId() : $langcode;

    $key = $machine_name . ':' . $langcode;
    if (empty($views[$key])) {
      $views[$key] = new ViewableGlobalContent($machine_name, $langcode);

      // If no global_content uis yet defined, return the machine name.
      $this->addViewStatistics($machine_name, $langcode);
    }
    return $views[$key];
  }

  /**
   * @param $machine_name
   * @param null $langcode
   *
   * @return array
   */
  public function getLazyBuilder($machine_name, $langcode = NULL) {
    $langcode = (empty($langcode)) ? \Drupal::languageManager()
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
      ->getId() : $langcode;
    return [
      '#lazy_builder' => [
        'global_content:buildGlobalContent',
        [
          'machine_name' => $machine_name,
          'langcode' => $langcode,
        ],
      ],
      '#create_placeholder' => TRUE,
    ];
  }

  public function buildGlobalContent($machine_name, $langcode) {
    return $this->get($machine_name, $langcode)->toRenderable();
  }

  public function addViewStatistics($machine_name, $langcode = NULL) {
    // Add location of where Global Content is called for statistics.
    $requestUri = $this->requestStack->getCurrentRequest()->getRequestUri();

  }
}