View source
<?php
namespace Drupal\freelinking_prepopulate\Plugin\freelinking;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\freelinking\Plugin\FreelinkingPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FreelinkingPrepopulate extends FreelinkingPluginBase implements ContainerFactoryPluginInterface {
protected $entityFieldManager;
protected $entityTypeManager;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entityFieldManager, ModuleHandlerInterface $moduleHandler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
$this->entityFieldManager = $entityFieldManager;
$this->moduleHandler = $moduleHandler;
}
public function getConfiguration() {
$configuration = parent::getConfiguration();
return NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
public function defaultConfiguration() {
$node_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$default = reset($node_types);
return [
'settings' => [
'default_node_type' => $default
->id(),
'advanced' => [
'title' => FALSE,
],
'failover' => 'search',
],
];
}
public function getIndicator() {
return '/^create(node)?$/';
}
public function getTip() {
return $this
->t('Links to a prepopulated node/add form.');
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$config = $this
->getConfiguration();
$element['failover'] = [
'#type' => 'select',
'#title' => $this
->t('If path alias is not found'),
'#description' => $this
->t('What should freelinking do when the page is not found?'),
'#options' => [
'error' => $this
->t('Insert an error message'),
],
'#default_value' => $config['settings']['failover'],
];
if ($this->moduleHandler
->moduleExists('search')) {
$element['failover']['#options']['search'] = $this
->t('Add link to search content');
}
$node_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$options = array_reduce($node_types, function (&$result, $node_type) {
$result[$node_type
->id()] = $node_type
->label();
return $result;
}, []);
$element['default_node_type'] = [
'#type' => 'select',
'#title' => $this
->t('Content type'),
'#description' => $this
->t('Choose the default node type to use for Node Create links.'),
'#options' => $options,
'#required' => TRUE,
'#default_value' => $config['settings']['default_node_type'],
];
return $element;
}
public function buildLink(array $target) {
$config = $this
->getConfiguration();
$title = isset($target['text']) ? $target['text'] : $target['dest'];
if (isset($target['type']) || isset($target['bundle'])) {
$bundle_name = isset($target['type']) ? $target['type'] : $target['bundle'];
}
else {
$bundle_name = $config['settings']['default_node_type'];
}
$route_params = [
'node_type' => $bundle_name,
];
$options = [
'query' => [
'edit[title][widget][0][value]' => Xss::filter($title, []),
],
];
$fields = $this->entityFieldManager
->getFieldDefinitions('node', $bundle_name);
$blacklist = [
'type',
'bundle',
'text',
'dest',
];
foreach ($fields as $field_name => $field_definition) {
if (!in_array($field_name, $blacklist) && array_key_exists($field_name, $target) && !$field_definition
->isInternal() && !$field_definition
->isComputed() && !$field_definition
->isReadOnly()) {
$storage_definition = $field_definition
->getFieldStorageDefinition();
if ($storage_definition) {
$prop = $storage_definition
->getMainPropertyName();
if ($storage_definition
->getType() === 'entity_reference') {
$key = '[' . $prop . ']';
}
else {
$key = '[0][' . $prop . ']';
}
$query_name = 'edit[' . $field_name . '][widget]' . $key;
$options['query'][$query_name] = Xss::filter($target[$field_name]);
}
}
}
$this->moduleHandler
->alter('freelinking_prepopulate_query', $options['query'], $target);
$url = Url::fromRoute('node.add', $route_params, $options);
if ($url
->access()) {
$link = [
'#type' => 'link',
'#title' => $title,
'#url' => $url,
'#attributes' => [
'title' => $this
->getTip(),
],
];
}
elseif ($config['settings']['failover'] === 'search') {
$link = [
'#type' => 'link',
'#title' => $title,
'#url' => Url::fromUserInput('/search', [
'query' => [
'keys' => $title,
],
'language' => $target['language'],
]),
];
}
else {
$link = [
'#theme' => 'freelink_error',
'#plugin' => 'freelinking_prepopulate',
'#message' => $this
->t('Access denied to create missing content.'),
];
}
return $link;
}
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_field.manager'), $container
->get('module_handler'));
}
}