You are here

trait WeatherDisplayBlockTrait in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Block/WeatherDisplayBlockTrait.php \Drupal\weather\Plugin\Block\WeatherDisplayBlockTrait

Provides reusable functions for weather block plugins.

Hierarchy

File

src/Plugin/Block/WeatherDisplayBlockTrait.php, line 18

Namespace

Drupal\weather\Plugin\Block
View source
trait WeatherDisplayBlockTrait {

  /**
   * Weather display to show in this block.
   *
   * @var \Drupal\weather\Entity\WeatherDisplayInterface
   */
  protected $weatherDisplay;

  /**
   * Weather display Place storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $weatherDisplayPlaceStorage;

  /**
   * Current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * Block type. Either 'user' or 'system-wide'.
   *
   * @var string
   */
  protected $destination;

  /**
   * {@inheritdoc}
   */
  public function build() {
    if (!$this->weatherDisplay instanceof WeatherDisplayInterface) {
      return [];
    }
    $type = $this->weatherDisplay->type->value;
    $number = $this->weatherDisplay->number->value;
    $display_places = $this->weatherDisplayPlaceStorage
      ->loadByProperties([
      'display_type' => $type,
      'display_number' => $number,
    ]);
    if (empty($display_places)) {
      return [];
    }
    $build = [
      '#theme' => 'weather',
      '#display_type' => $type,
      '#display_number' => $number,
      '#destination' => $this->destination,
    ];
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {

    // Update this block after related weather_display
    // or one of related weather_display_places updated.
    $parent = parent::getCacheTags();
    $displayTags = $this->weatherDisplay
      ->getCacheTags();
    $tags = Cache::mergeTags($parent, $displayTags);
    $display_places = $this->weatherDisplayPlaceStorage
      ->loadByProperties([
      'display_type' => $this->weatherDisplay->type->value,
      'display_number' => $this->weatherDisplay->number->value,
    ]);
    foreach ($display_places as $display_place) {
      $tags = Cache::mergeTags($tags, $display_place
        ->getCacheTags());
    }

    // Depends on weather module settings also.
    $tags = Cache::mergeTags($tags, [
      'config:weather.settings',
    ]);
    return $tags;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WeatherDisplayBlockTrait::$currentUser protected property Current user.
WeatherDisplayBlockTrait::$destination protected property Block type. Either 'user' or 'system-wide'.
WeatherDisplayBlockTrait::$weatherDisplay protected property Weather display to show in this block.
WeatherDisplayBlockTrait::$weatherDisplayPlaceStorage protected property Weather display Place storage.
WeatherDisplayBlockTrait::build public function
WeatherDisplayBlockTrait::getCacheTags public function