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 / ViewableGlobalContent.php
Size: Mime:
<?php

namespace Drupal\dds_global_content;

use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\RenderableInterface;
use Drupal\Core\Render\Renderer;

class ViewableGlobalContent implements RenderableInterface, CacheableDependencyInterface {

  use DependencySerializationTrait;

  protected $machine_name = '';

  protected $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;

  /** @var GlobalContentPluginInterface */
  protected $plugin;

  protected $type = '';

  /** @var GlobalContent */
  protected $globalContent;

  protected $rendered;

  protected $options = [];

  protected $token_data = [];

  /** @var CacheableMetadata */
  protected $cacheabilityMetadata;

  protected $inline = FALSE;

  /**
   * ViewableGlobalContent constructor.
   *
   * @param string $machine_name
   */
  public function __construct($machine_name, $langcode = NULL) {
    $this->langcode = $langcode;
    $this->globalContent = GlobalContent::load($machine_name, $langcode);
    if (!empty($this->globalContent)) {
      $this->type = $this->globalContent->getType();
      $this->options = $this->globalContent->getOptions();
      $this->plugin = $this->globalContent->getPlugin();
      if (!empty($this->plugin)) {
        if ($this->plugin->getRenderSettings('inline')) {
          $this->inline = TRUE;
        }
      }
    }
  }

  /**
   * @return string
   */
  public function getType() {
    return $this->type;
  }

  /**
   * @return mixed
   */
  public function getData() {
    if (empty($this->getGlobalContent())) {
      return [];
    }
    return $this->getGlobalContent()->getData();
  }

  /**
   * @return array
   */
  public function getOptions() {
    if (empty($this->getGlobalContent())) {
      return [];
    }
    return $this->getGlobalContent()->getOptions();
  }

  /**
   * @return string
   */
  public function getLangCode() {
    if (empty($this->langcode)) {
      $this->langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
    }
    return $this->langcode;
  }

  /**
   * @return CacheableMetadata
   */
  public function getCacheableMetadata() {
    if (empty($this->cacheabilityMetadata)) {
      $this->cacheabilityMetadata = CacheableMetadata::createFromRenderArray($this->getRendered());
      $this->cacheabilityMetadata->addCacheableDependency($this->getGlobalContent());
    }
    return $this->cacheabilityMetadata;
  }

  /**
   * @param array $data
   *
   * @return self
   */
  public function setTokenData(array $data) {
    $this->token_data = $data;
    $this->rendered = NULL;
    return $this;
  }

  /**
   * @param string $key
   * @param mixed $value
   *
   * @return self
   */
  public function setOption($key, $value) {
    $options = $this->getGlobalContent()->getOptions();
    $options[$key] = $value;
    return $this->setOptions($options);
  }

  /**
   * @param array $options
   *
   * @return self
   */
  public function setOptions(array $options) {
    $this->getGlobalContent()->setOptions($options);
    $this->rendered = NULL;
    return $this;
  }

  /**
   * @return array
   */
  public function getTokenData() {
    return $this->token_data;
  }

  /**
   * @deprecated Use toRenderable() or toRenderableInline() instead
   * @return array
   */
  public function asRenderable() {
    if ($this->inline) {
      return $this->toRenderableInline();
    } else {
      return $this->toRenderable();
    }
  }

  /**
   * @deprecated Use __toString() instead
   * @return string
   */
  public function asString() {
    return $this->__toString();
  }

  /**
   * @return string
   */
  public function toString() {
    return $this->__toString();
  }

  /**
   * @return string
   */
  public function __toString() {
    /** @var Renderer $renderer */
    static $renderer = NULL;
    if (is_null($renderer)) {
      $renderer = \Drupal::service('renderer');
    }
    $rendered = $this->getRendered();
    $string = $renderer->renderPlain($rendered);
    if (is_string($string)) {
      return $string;
    } else {
      return $string->__toString();
    }
  }

  /**
   * Returns a render array representation of the object.
   *
   * @return array
   *   A render array.
   */
  public function toRenderable() {
    $build = [
      '#theme' => 'global_content',
      '#global_content' => $this->getGlobalContent(),
    ];
    $build[] = $this->getRendered();
    $this->getCacheableMetadata()->applyTo($build);
    $build['#theme_wrappers'] = ['global_content_element'];
    if (!empty($this->getGlobalContent())) {
      $build['#contextual_links'] = [
        'global_content' => [
          'route_parameters' => ['group_id' => $this->getGlobalContent()->getGroupID()],
        ],
      ];
    }
    return $build;
  }

  /**
   * Returns a render array representation of the object, but inline.
   *
   * @return array
   *   A render array.
   */
  public function toRenderableInline() {
    $build = $this->getRendered();
    $this->getCacheableMetadata()->applyTo($build);
    return $build;
  }

  /**
   * @param string $key
   *
   * @return mixed
   */
  public function toValue($key = '') {
    return $this->getGlobalContent()->getPlugin()->value($this, $key);
  }

  public function getGlobalContent() {
    if (empty($this->globalContent)) {
      $this->globalContent = GlobalContent::load($this->machine_name, $this->langcode);
    }
    return $this->globalContent;
  }

  /**
   * @return array
   *   A render array.
   */
  private function getRendered() {
    if (empty($this->rendered)) {
      if (!empty($this->plugin)) {
        $this->rendered = $this->plugin->render($this);
      }
      if (empty($this->rendered)) {
        $this->rendered = $this->toEmptyRenderable();
      }
    }
    return $this->rendered;
  }

  private function toEmptyRenderable() {
    return ['#plain_text' => $this->machine_name];
  }

  /**
   * The cache contexts associated with this object.
   *
   * These identify a specific variation/representation of the object.
   *
   * Cache contexts are tokens: placeholders that are converted to cache keys by
   * the @cache_contexts_manager service. The replacement value depends on the
   * request context (the current URL, language, and so on). They're converted
   * before storing an object in cache.
   *
   * @return string[]
   *   An array of cache context tokens, used to generate a cache ID.
   *
   * @see \Drupal\Core\Cache\Context\CacheContextsManager::convertTokensToKeys()
   */
  public function getCacheContexts() {
    return $this->getCacheableMetadata()->getCacheContexts();
  }

  /**
   * The cache tags associated with this object.
   *
   * When this object is modified, these cache tags will be invalidated.
   *
   * @return string[]
   *   A set of cache tags.
   */
  public function getCacheTags() {
    return $this->getCacheableMetadata()->getCacheTags();
  }

  /**
   * The maximum age for which this object may be cached.
   *
   * @return int
   *   The maximum time in seconds that this object may be cached.
   */
  public function getCacheMaxAge() {
    return $this->getCacheableMetadata()->getCacheMaxAge();
  }

}