Last active
September 29, 2020 19:55
-
-
Save fredysan/fbed8969e4abc65895a65f27311f6567 to your computer and use it in GitHub Desktop.
License Agreement Modal Block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\license_modal\Plugin\Block; | |
use Drupal\license_modal\Form\AcceptLicenseForm; | |
use Drupal\Core\Block\BlockBase; | |
use Drupal\Core\Entity\EntityStorageInterface; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
use Drupal\Core\Session\AccountInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Provides a block to display the license agrement. | |
* | |
* @Block( | |
* id = "license_modal_block", | |
* admin_label = @Translation("License Modal Block"), | |
* ) | |
*/ | |
class LicenseModalBlock extends BlockBase implements ContainerFactoryPluginInterface { | |
protected $currentUser; | |
protected $nodeStorage; | |
protected $userStorage; | |
/** | |
* Constructs a \Drupal\views\Plugin\Block\ViewsBlockBase object. | |
* | |
* @param array $configuration | |
* A configuration array containing information about the plugin instance. | |
* @param string $plugin_id | |
* The plugin_id for the plugin instance. | |
* @param mixed $plugin_definition | |
* The plugin implementation definition. | |
* @param \Drupal\Core\Entity\EntityStorageInterface $nodeStorage | |
* The nodes storage. | |
* @param \Drupal\Core\Entity\EntityStorageInterface $nodeStorage | |
* The users storage. | |
* @param \Drupal\Core\Session\AccountInterface $user | |
* The current user. | |
*/ | |
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $nodeStorage, EntityStorageInterface $userStorage, AccountInterface $user) { | |
parent::__construct($configuration, $plugin_id, $plugin_definition); | |
$this->currentUser = $user; | |
$this->nodeStorage = $nodeStorage; | |
$this->userStorage = $userStorage; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
return new static( | |
$configuration, $plugin_id, $plugin_definition, | |
$container->get('entity_type.manager')->getStorage('node'), | |
$container->get('entity_type.manager')->getStorage('user'), | |
$container->get('current_user') | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function blockForm($form, FormStateInterface $form_state) { | |
$form = parent::blockForm($form, $form_state); | |
$config = $this->getConfiguration(); | |
$node = NULL; | |
if (isset($config['license_node'])) { | |
$node = $this->nodeStorage->load($config['license_node']); | |
} | |
$form['license_node'] = [ | |
'#type' => 'entity_autocomplete', | |
'#title' => t('License Agreement Page'), | |
'#target_type' => 'node', | |
'#required' => TRUE, | |
'#default_value' => $node, | |
'#selection_settings' => [ | |
'target_bundles' => ['page'], | |
], | |
]; | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function blockSubmit($form, FormStateInterface $form_state) { | |
parent::blockSubmit($form, $form_state); | |
$values = $form_state->getValues(); | |
$this->configuration['license_node'] = $values['license_node']; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function build() { | |
$build = []; | |
if (!$this->currentUser->isAnonymous()) { | |
$account = $this->userStorage->load($this->currentUser->id()); | |
if ($account->get('field_date_accepted_license')->isEmpty()) { | |
$config = $this->getConfiguration(); | |
$node = isset($config['license_node']) | |
? $this->nodeStorage->load($config['license_node']) | |
: NULL; | |
$form = \Drupal::formBuilder()->getForm(AcceptLicenseForm::class); | |
$build = [ | |
'#theme' => 'license_modal', | |
'#node' => $node, | |
'#form' => $form, | |
"#attached" => [ | |
'library' => 'license_modal/modal', | |
], | |
]; | |
} | |
} | |
return $build; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment