View source
<?php
namespace Drupal\varbase_total_control\Plugin\Block;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class VarbaseCreateContent extends BlockBase implements BlockPluginInterface, ContainerFactoryPluginInterface {
protected $stringTranslation;
protected $entityTypeManager;
protected $renderer;
protected $redirectDestination;
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, RedirectDestinationInterface $redirect_destination, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->stringTranslation = $string_translation;
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
$this->redirectDestination = $redirect_destination;
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('string_translation'), $container
->get('entity_type.manager'), $container
->get('renderer'), $container
->get('redirect.destination'), $container
->get('current_user'), $container
->get('entity_type.manager')
->getStorage('user'));
}
public function build() {
$types = array_reverse($this->entityTypeManager
->getStorage('node_type')
->loadMultiple());
$links = [];
$config = $this
->getConfiguration();
$destination = $this->redirectDestination
->getAsArray();
$options = [
$destination,
];
foreach ($types as $type => $object) {
if (!array_key_exists($type, $config['total_control_admin_types_links']) || (isset($config['total_control_admin_types_links']) && $config['total_control_admin_types_links'][$type]) == $type) {
if ($this->currentUser
->hasPermission('create ' . $object
->get('type') . ' content')) {
$link_options = [
'attributes' => [
'class' => [
Html::cleanCssIdentifier(mb_strtolower($object
->get('type'))),
],
],
];
$url = new Url('node.add', [
'node_type' => $object
->get('type'),
], $options);
$url
->setOptions($link_options);
$links[] = Link::fromTextAndUrl($object
->get('name'), $url);
}
}
}
$links[] = Link::fromTextAndUrl($this
->t('More...'), new Url('node.add_page', $options));
$body_data = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#items' => $links,
];
$markup_data = $this->renderer
->render($body_data);
return [
'#type' => 'markup',
'#markup' => $markup_data,
];
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this
->getConfiguration();
$types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$type_defaults = [];
foreach ($types as $type => $object) {
if (!array_key_exists($type, $type_defaults)) {
$type_defaults[$type] = $type;
}
}
$form['total_control_admin_types_links'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Include Create links for Content Types'),
'#options' => $type_defaults,
'#default_value' => $config['total_control_admin_types_links'],
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
parent::blockSubmit($form, $form_state);
$values = $form_state
->getValues();
$this->configuration['total_control_admin_types_links'] = $values['total_control_admin_types_links'];
}
}