You are here

DevelExecutePHP.php in Devel PHP 8

File

src/Plugin/Block/DevelExecutePHP.php
View source
<?php

declare (strict_types=1);
namespace Drupal\devel_php\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a block for executing PHP code.
 *
 * @Block(
 *   id = "devel_execute_php",
 *   admin_label = @Translation("Execute PHP Code")
 * )
 */
class DevelExecutePHP extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The form builder service.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * Constructor.
   *
   * @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\Form\FormBuilderInterface $formBuilder
   *   The form builder service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $formBuilder) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->formBuilder = $formBuilder;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
    return new self($configuration, $plugin_id, $plugin_definition, $container
      ->get('form_builder'));
  }

  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    return AccessResult::allowedIfHasPermission($account, 'execute php code');
  }

  /**
   * {@inheritdoc}
   */
  public function build() {

    // @phpstan-ignore-next-line
    return $this->formBuilder
      ->getForm('Drupal\\devel_php\\Form\\ExecutePHP', FALSE);
  }

}

Classes

Namesort descending Description
DevelExecutePHP Provides a block for executing PHP code.