google_qr_code_block.php in Google QR Code Generator 8
File
src/Plugin/Block/google_qr_code_block.php
View source
<?php
namespace Drupal\google_qr_code\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Url;
use Drupal\Core\Link;
class google_qr_code_block extends BlockBase {
public function build() {
$render_type = \Drupal::config('google_qr_code.settings')
->get('whenshow');
$google_qr_current_url = \Drupal::request()
->getUri();
$qr_code_height = \Drupal::config('google_qr_code.settings')
->get('height');
$qr_code_width = \Drupal::config('google_qr_code.settings')
->get('width');
if ($render_type == 'on_click') {
$requested_url_obj = Url::fromUri($google_qr_current_url);
$link_object = Link::fromTextAndUrl(t('Click to see QR Code for this URL'), $requested_url_obj);
$output = $link_object
->toRenderable();
$output['#class'] = 'google-qr-code-link';
$output['#cache'] = array(
'contexts' => array(
'url.path',
),
);
$output['#attached']['library'][] = 'google_qr_code/google-qr-code-js';
$output['#attached']['drupalSettings']['google_qr_code_url'] = $google_qr_current_url;
$output['#attached']['drupalSettings']['google_qr_code_height'] = $qr_code_height;
$output['#attached']['drupalSettings']['google_qr_code_width'] = $qr_code_width;
return $output;
}
else {
$google_qr_image_url = "https://chart.googleapis.com/chart?chs=" . $qr_code_width . "x" . $qr_code_height . "&cht=qr&chl=" . $google_qr_current_url . '&chld=H|0';
$google_qr_alt = $this
->t('QR Code for @url', array(
'@url' => $google_qr_current_url,
));
return array(
'#theme' => 'image',
'#uri' => $google_qr_image_url,
'#width' => $qr_code_width,
'#height' => $qr_code_height,
'#alt' => $google_qr_alt,
'#class' => 'google-qr-code-image',
'#cache' => array(
'contexts' => array(
'url.path',
),
),
);
}
}
public function blockAccess(AccountInterface $account) {
return AccessResult::allowed();
}
}