You are here

public function PageUrlQrCodeBlock::build in Page URL QR Code Block 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/PageUrlQrCodeBlock.php, line 101
Contains \Drupal\page_url_qr_code_block\Plugin\Block\page_url_qr_code_block.

Class

PageUrlQrCodeBlock
Provides my custom block.

Namespace

Drupal\page_url_qr_code_block\Plugin\Block

Code

public function build() {
  global $base_url;
  $path = \Drupal::request()
    ->getRequestUri();
  $path = \Drupal::service('path.alias_manager')
    ->getAliasByPath($path);
  $width = $this->configuration['page_url_qr_code_width_height'];
  $api = $this->configuration['page_url_qr_code_api'];
  $page_url = urlencode($base_url . $path);
  if ('liantu' == $api) {
    $url = "//qr.liantu.com/api.php?bg=ffffff&w={$width}&text={$page_url}";
  }
  else {
    $url = "//chart.apis.google.com/chart?chs={$width}x{$width}&cht=qr&chl={$page_url}";
  }
  return array(
    '#theme' => 'page_url_qr_code_block',
    '#url' => $url,
    '#alt' => $this->configuration['page_url_qr_code_alt'],
    '#width' => $width,
    '#height' => $this->configuration['page_url_qr_code_width_height'],
    '#caption' => $this->configuration['page_url_qr_code_caption'],
    '#attached' => array(
      'library' => array(
        'page_url_qr_code_block/page_url_qr_code_block_style',
      ),
    ),
    '#cache' => [
      'contexts' => [
        'url',
      ],
      'max-age' => 0,
    ],
  );
}