You are here

public function OpenReadspeakerBlock::build in Open ReadSpeaker 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/OpenReadspeakerBlock.php, line 112

Class

OpenReadspeakerBlock
Provides a 'OpenReadspeakerBlock' block.

Namespace

Drupal\open_readspeaker\Plugin\Block

Code

public function build() {
  $config = $this->configFactory
    ->get('open_readspeaker.settings');
  $accountid = $config
    ->get('open_readspeaker_accountid');
  $post_mode = $config
    ->get('open_readspeaker_post_mode');
  $url = Url::fromRoute('<current>', [
    'query' => \Drupal::request()->query
      ->all(),
  ], [
    'absolute' => TRUE,
  ])
    ->toString();
  $build = [];
  if (empty($accountid)) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Please go to @link and fill the account id.', [
      '@link' => Link::createFromRoute($this
        ->t('manage open ReadSpeaker'), 'open_readspeaker.settings')
        ->toString(),
    ]));
    return $build;
  }
  if ($post_mode) {
    $library[] = 'open_readspeaker/post_mode';
  }
  $library[] = 'open_readspeaker/basic';
  $settings['open_readspeaker'] = [
    'accountid' => $accountid,
  ];
  $build['open_readspeaker_block'] = [
    '#theme' => 'open_readspeaker_ui',
    '#accountid' => $accountid,
    '#open_readspeaker_i18n' => $config
      ->get('open_readspeaker_i18n'),
    '#request_path' => $url,
    '#custom_style' => isset($this->configuration['open_readspeaker_buttonstyle']) ? $this->configuration['open_readspeaker_buttonstyle'] : 'open-readspeaker-button',
    '#button_text' => isset($this->configuration['open_readspeaker_buttontext']) ? $this->configuration['open_readspeaker_buttontext'] : $this
      ->t('Listen'),
    '#button_title' => isset($this->configuration['open_readspeaker_buttontitle']) ? $this->configuration['open_readspeaker_buttontitle'] : $this
      ->t('Listen to this page using ReadSpeaker'),
    '#open_readspeaker_reading_area' => isset($this->configuration['open_readspeaker_reading_area']) ? $this->configuration['open_readspeaker_reading_area'] : 'rs_read_this',
    '#open_readspeaker_reading_area_class' => isset($this->configuration['open_readspeaker_reading_area_class']) ? $this->configuration['open_readspeaker_reading_area_class'] : 'rs_read_this_class',
    '#attached' => [
      'library' => $library,
      'drupalSettings' => $settings,
    ],
    '#cache' => [
      'contexts' => [
        'url',
      ],
    ],
  ];
  return $build;
}