page_tester-1.0.x-dev/src/Plugin/Block/PageTesterBlock.php
src/Plugin/Block/PageTesterBlock.php
<?php
namespace Drupal\page_tester\Plugin\Block;
use Drupal;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\page_tester\Form\PageTesterForm;
use Drupal\page_tester\TestsPluginManager;
/**
* Provides a 'Page Tester' Block.
*/
#[Block(
id: "page_tester_block",
admin_label: new TranslatableMarkup("Page Tester"),
category: new TranslatableMarkup("Page Tester")
)]
class PageTesterBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Constructs a Drupalist 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 mixed $testsPluginManager
* The tests plugin implementation manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected TestsPluginManager $testsPluginManager,
protected EntityTypeManagerInterface $entityTypeManager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('plugin.manager.tests'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'page_tester_block' => $this->t(''),
];
}
/**
* Helper function to populate the tests list.
*
* @return array
* Dropdown options.
*/
private function getTests() {
$tests = [];
$page_tester_test = $this->entityTypeManager->getStorage('page_tester_test');
$nodes = $page_tester_test->loadMultiple();
foreach ($nodes as $node) {
$tests[$node->id()] = $node->label();
}
return $tests;
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['page_tester_block_test'] = [
'#type' => 'select',
'#required' => TRUE,
'#title' => $this->t('Tests Plugin'),
'#options' => $this->getTests(),
'#default_value' => $this->configuration['page_tester_block_test'] ?? '',
];
$form['page_tester_block_test_type'] = [
'#type' => 'select',
'#required' => TRUE,
'#title' => $this->t('Tests Plugin Type'),
'#options' => [
'manual' => 'Manual',
'automated' => 'Automated'
],
'#default_value' => $this->configuration['page_tester_block_test_type'] ?? 'manual',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$this->setConfigurationValue('page_tester_block_test', $values['page_tester_block_test']);
$this->setConfigurationValue('page_tester_block_test_type', $values['page_tester_block_test_type']);
}
/**
* {@inheritdoc}
*/
public function build() {
$options = [
'test' => $this->configuration['page_tester_block_test'],
'type' => $this->configuration['page_tester_block_test_type'],
];
$build['page_tester'] = \Drupal::formBuilder()->getForm(PageTesterForm::class, $options);
return $build;
}
}
