View source
<?php
namespace Drupal\panopoly_widgets\Plugin\Block;
use Drupal\node\Entity\NodeType;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentItemBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $entityDisplayRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityDisplayRepository = $entity_display_repository;
}
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'), $container
->get('entity_display.repository'));
}
public function defaultConfiguration() {
return [
'nid' => NULL,
'view_mode' => 'default',
];
}
protected function loadEntity() {
if (!empty($this->configuration['nid'])) {
$storage = $this->entityTypeManager
->getStorage('node');
return $storage
->load($this->configuration['nid']);
}
return NULL;
}
public function blockForm($form, FormStateInterface $form_state) {
$entity = $this
->loadEntity();
$content_types = $this
->getContentTypes();
$form['#attached'] = [
'library' => [
'panopoly_widgets/content-item',
],
];
$form['content_type'] = [
'#type' => 'select',
'#options' => array_merge([
'all' => 'Any',
], $content_types),
'#title' => $this
->t('Content type'),
'#default_value' => $entity ? $entity
->bundle() : 'all',
'#ajax' => [
'callback' => [
$this,
'autocompleteGetNodes',
],
],
];
$form['node'] = [
'#prefix' => '<div id="node-selector-wrapper">',
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Piece of content'),
'#target_type' => 'node',
'#default_value' => $entity,
'#required' => TRUE,
'#selection_settings' => [
'target_bundles' => array_keys($content_types),
],
'#suffix' => '</div>',
'#attributes' => [
'class' => [
'js-panopoly-widgets-content-item-autocomplete',
],
],
];
$form['view_mode'] = [
'#type' => 'radios',
'#options' => $this->entityDisplayRepository
->getViewModeOptions('node'),
'#title' => $this
->t('View mode'),
'#default_value' => $this->configuration['view_mode'],
];
return $form;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['label']['#attributes'] = [
'class' => [
'js-panopoly-widgets-content-item-label',
],
];
return $form;
}
private function getContentTypes() {
$node_types = NodeType::loadMultiple();
$options = [];
foreach ($node_types as $node_type) {
$options[$node_type
->id()] = $node_type
->label();
}
return $options;
}
public function autocompleteGetNodes(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new InvokeCommand(NULL, 'panopolyWidgetsCleanNodeAutoComplete', []));
return $response;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['nid'] = $form_state
->getValue('node');
$this->configuration['view_mode'] = $form_state
->getValue('view_mode');
}
public function build() {
$view_builder = $this->entityTypeManager
->getViewBuilder('node');
$build = [];
if ($entity = $this
->loadEntity()) {
$entity->title = '';
$build = $view_builder
->view($entity, $this->configuration['view_mode']);
CacheableMetadata::createFromObject($entity)
->applyTo($build);
$build['#title'] = Link::fromTextAndUrl($this->configuration['label'], $entity
->toUrl());
}
return $build;
}
}