View source
<?php
namespace Drupal\trumba\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Cache\CacheTagsInvalidator;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
abstract class TrumbaBlockBase extends BlockBase implements ContainerFactoryPluginInterface {
public $defaultTrumbaWebName;
public $spudId;
public $cacheInvalidator;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CacheTagsInvalidator $cacheInvalidator, UuidInterface $uuid_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->defaultTrumbaWebName = \Drupal::config('trumba.trumbaconfiguration')
->get('default_web_name');
$this->spudId = $this
->getBaseId() . '-' . $uuid_service
->generate();
$this->cacheInvalidator = $cacheInvalidator;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('cache_tags.invalidator'), $container
->get('uuid'));
}
public function blockForm($form, FormStateInterface $form_state) {
$form['trumba_web_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Web Name'),
'#description' => $this
->t('This is the unique identifier for your calendar account on Trumba.'),
'#default_value' => isset($this->configuration['trumba_web_name']) ? $this->configuration['trumba_web_name'] : $this->defaultTrumbaWebName,
'#maxlength' => 255,
'#size' => 64,
'#weight' => '1',
'#required' => TRUE,
];
$form['trumba_same_page'] = [
'#type' => 'radios',
'#title' => $this
->t('Is this block on the same page as the Main Calendar?'),
'#default_value' => isset($this->configuration['trumba_same_page']) ? $this->configuration['trumba_same_page'] : 0,
'#options' => array(
0 => $this
->t('Yes'),
1 => $this
->t('No'),
),
];
$form['trumba_spud_url'] = [
'#type' => 'textfield',
'#title' => $this
->t('Calendar URL'),
'#description' => $this
->t('Enter the internal path (e.g.: "/node/1") or the full URL for where this calendar will be placed (e.g.: "https://www.yoursite.com/calendar").'),
'#default_value' => isset($this->configuration['trumba_spud_url']) && !empty($this->configuration['trumba_spud_url']) ? $this
->convertUriToRelativePathOrUrl($this->configuration['trumba_spud_url']) : '',
'#maxlength' => 255,
'#size' => 64,
'#weight' => '2',
'#states' => [
'invisible' => [
':input[name="settings[trumba_same_page]"]' => [
'value' => 0,
],
],
'required' => [
':input[name="settings[trumba_same_page]"]' => [
'value' => 1,
],
],
],
];
return $form;
}
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIfHasPermission($account, 'view trumba spud blocks');
}
public function blockValidate($form, FormStateInterface $form_state) {
$rawUrl = $form_state
->getValue('trumba_spud_url');
$firstChar = substr($rawUrl, 0, 1);
if (!empty($rawUrl) && !$this
->isExternalUri($rawUrl) && $firstChar != '/') {
$form_state
->setErrorByName('trumba_spud_url', $this
->t('Internal Url\'s must begin with a forward slash.'));
}
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->cacheInvalidator
->invalidateTags([
'trumba:' . $this->spudId,
]);
$this->configuration['trumba_web_name'] = $form_state
->getValue('trumba_web_name');
$this->configuration['trumba_same_page'] = $form_state
->getValue('trumba_same_page');
$this->configuration['trumba_spud_url'] = $this
->convertInputToUriString($form_state
->getValue('trumba_spud_url'));
}
public function convertUriToAbsolutePathOrUrl($uri) {
return $uri ? Url::fromUri($uri)
->setAbsolute()
->toString() : '';
}
public function convertUriToRelativePathOrUrl($uri) {
return $uri ? Url::fromUri($uri)
->toString() : '';
}
public function convertInputToUriString($input) {
$uri = '';
if ($input) {
if ($this
->isExternalUri($input)) {
$uri = Url::fromUri($input)
->toUriString();
}
else {
$uri = Url::fromUserInput($input)
->toUriString();
}
}
return $uri;
}
public function isExternalUri($uri) {
$parts = parse_url($uri);
return !empty($parts['host']) ? TRUE : FALSE;
}
}