Skip to content
Snippets Groups Projects
RSVPBlock.php 1.17 KiB
Newer Older
Alexis Grolot's avatar
Alexis Grolot committed
<?php
/**
Alexis Grolot's avatar
Alexis Grolot committed
 * @file
 * Contains \Drupal\rsvp_module\Plugin\Block\RSVPBlock
 */
 namespace Drupal\rsvp_module\Plugin\Block;
 
 use Drupal\Core\Block\BlockBase;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\Access\AccessResult;
Alexis Grolot's avatar
Alexis Grolot committed
 /**
 * Provides a 'RSVP' List Block
 *
 * @Block(
 *   id = "rsvp_block",
 *   admin_label = @Translation("RSVP Block"),
 *   category = @Translation("Blocks")
 * )
 */
class RSVPBlock extends BlockBase {
Alexis Grolot's avatar
Alexis Grolot committed

  /**
Alexis Grolot's avatar
Alexis Grolot committed
   * {@inheritdoc}
   */
Alexis Grolot's avatar
Alexis Grolot committed
  public function build() {
Alexis Grolot's avatar
Alexis Grolot committed
    return \Drupal::formBuilder()->getForm('Drupal\rsvp_module\Form\RSVPForm');
    }

  /**
   * {@inheritdoc}
   */
Alexis Grolot's avatar
Alexis Grolot committed
  public function blockAccess(AccountInterface $account) {
Alexis Grolot's avatar
Alexis Grolot committed
    /** @var \Drupal\node\Entity\Node $node */
    $node = \Drupal::routeMatch()->getParameter('node');
    $nid = $node->nid->value;
    /** @var \Drupal\rsvp_module\EnablerService $enabler */
    $enabler = \Drupal::service('rsvp_module.enabler');
    if(is_numeric($nid)) {
      if($enabler->isEnabled($node)) {
        return AccessResult::allowedIfHasPermission($account, 'view rsvp_module');
      }
    }
    return AccessResult::forbidden();
Alexis Grolot's avatar
Alexis Grolot committed
  }
Alexis Grolot's avatar
Alexis Grolot committed
  
Alexis Grolot's avatar
Alexis Grolot committed
}
Alexis Grolot's avatar
Alexis Grolot committed