added linkit contrib module
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Annotation\Attribute.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Annotation;
|
||||
|
||||
use Drupal\Component\Annotation\Plugin;
|
||||
|
||||
/**
|
||||
* Defines an attribute annotation object.
|
||||
*
|
||||
* Plugin Namespace: Plugin\Linkit\Attribute
|
||||
*
|
||||
* For a working example, see \Drupal\linkit\Plugin\Linkit\Attribute\Title
|
||||
*
|
||||
* @see \Drupal\linkit\AttributeInterface
|
||||
* @see \Drupal\linkit\AttributeBase
|
||||
* @see \Drupal\linkit\AttributeManager
|
||||
* @see plugin_api
|
||||
*
|
||||
* @Annotation
|
||||
*/
|
||||
class Attribute extends Plugin {
|
||||
|
||||
/**
|
||||
* The plugin ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* The human-readable name of the attribute.
|
||||
*
|
||||
* The string should be wrapped in a @Translation().
|
||||
*
|
||||
* @var \Drupal\Core\Annotation\Translation
|
||||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* The real HTML attribute name for this attribute.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $html_name;
|
||||
|
||||
/**
|
||||
* A brief description of the attribute.
|
||||
*
|
||||
* This will be shown when adding or configuring a profile.
|
||||
*
|
||||
* @ingroup plugin_translatable
|
||||
*
|
||||
* @var \Drupal\Core\Annotation\Translation (optional)
|
||||
*/
|
||||
public $description = '';
|
||||
|
||||
/**
|
||||
* A default weight for the attribute.
|
||||
*
|
||||
* @var int (optional)
|
||||
*/
|
||||
public $weight = 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Annotation\Matcher.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Annotation;
|
||||
|
||||
use Drupal\Component\Annotation\Plugin;
|
||||
|
||||
/**
|
||||
* Defines a matcher annotation object.
|
||||
*
|
||||
* Plugin Namespace: Plugin\Linkit\Matcher
|
||||
*
|
||||
* @see \Drupal\linkit\MatcherInterface
|
||||
* @see \Drupal\linkit\MatcherBase
|
||||
* @see \Drupal\linkit\MatcherManager
|
||||
* @see plugin_api
|
||||
*
|
||||
* @Annotation
|
||||
*/
|
||||
class Matcher extends Plugin {
|
||||
|
||||
/**
|
||||
* The plugin ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* The human-readable name of the matcher.
|
||||
*
|
||||
* The string should be wrapped in a @Translation().
|
||||
*
|
||||
* @var \Drupal\Core\Annotation\Translation
|
||||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* The entity type that is managed by this matcher.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $entity_type;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\AttributeBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Plugin\PluginBase;
|
||||
|
||||
/**
|
||||
* Provides a base class for attribute plugins.
|
||||
*
|
||||
* @see \Drupal\linkit\Annotation\Attribute
|
||||
* @see \Drupal\linkit\AttributeBase
|
||||
* @see \Drupal\linkit\AttributeManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
abstract class AttributeBase extends PluginBase implements AttributeInterface {
|
||||
|
||||
/**
|
||||
* The weight of the attribute compared to others in an attribute collection.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $weight = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
$this->setConfiguration($configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConfiguration() {
|
||||
return [
|
||||
'id' => $this->getPluginId(),
|
||||
'weight' => $this->weight,
|
||||
'settings' => $this->configuration,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setConfiguration(array $configuration) {
|
||||
$configuration += [
|
||||
'weight' => '0',
|
||||
'settings' => [],
|
||||
];
|
||||
$this->configuration = $configuration['settings'] + $this->defaultConfiguration();
|
||||
$this->weight = $configuration['weight'];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLabel() {
|
||||
return $this->pluginDefinition['label'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHtmlName() {
|
||||
return $this->pluginDefinition['html_name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDescription() {
|
||||
return $this->pluginDefinition['description'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getWeight() {
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setWeight($weight) {
|
||||
$this->weight = $weight;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\AttributeCollection.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
|
||||
|
||||
/**
|
||||
* A collection of attribute plugins.
|
||||
*/
|
||||
class AttributeCollection extends DefaultLazyPluginCollection {
|
||||
|
||||
/**
|
||||
* All possible attribute IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $definitions;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return \Drupal\linkit\AttributeInterface
|
||||
*/
|
||||
public function &get($instance_id) {
|
||||
return parent::get($instance_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sortHelper($aID, $bID) {
|
||||
$a_weight = $this->get($aID)->getWeight();
|
||||
$b_weight = $this->get($bID)->getWeight();
|
||||
if ($a_weight == $b_weight) {
|
||||
return strnatcasecmp($this->get($aID)->getLabel(), $this->get($bID)->getLabel());
|
||||
}
|
||||
|
||||
return ($a_weight < $b_weight) ? -1 : 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\AttributeInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Component\Plugin\ConfigurablePluginInterface;
|
||||
use Drupal\Component\Plugin\PluginInspectionInterface;
|
||||
|
||||
/**
|
||||
* Defines the interface for attributes plugins.
|
||||
*
|
||||
* @see \Drupal\linkit\Annotation\Attribute
|
||||
* @see \Drupal\linkit\AttributeBase
|
||||
* @see \Drupal\linkit\AttributeManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
interface AttributeInterface extends PluginInspectionInterface, ConfigurablePluginInterface {
|
||||
|
||||
/**
|
||||
* Returns the attribute label.
|
||||
*
|
||||
* @return string
|
||||
* The attribute label.
|
||||
*/
|
||||
public function getLabel();
|
||||
|
||||
/**
|
||||
* Returns the attribute description.
|
||||
*
|
||||
* @return string
|
||||
* The attribute description.
|
||||
*/
|
||||
public function getDescription();
|
||||
|
||||
/**
|
||||
* Returns the attribute html name. This is the name of the attribute
|
||||
* that will be inserted in the <code><a></code> tag.
|
||||
*
|
||||
* @return string
|
||||
* The attribute html name.
|
||||
*/
|
||||
public function getHtmlName();
|
||||
|
||||
/**
|
||||
* Returns the weight of the attribute.
|
||||
*
|
||||
* @return int|string
|
||||
* Either the integer weight of the attribute or an empty string.
|
||||
*/
|
||||
public function getWeight();
|
||||
|
||||
/**
|
||||
* Sets the weight for this attribute.
|
||||
*
|
||||
* @param int $weight
|
||||
* The weight for this attribute.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setWeight($weight);
|
||||
|
||||
/**
|
||||
* The form element structure for this attribute to be used in the dialog.
|
||||
*
|
||||
* @param mixed $default_value
|
||||
* The default value for the element. Used when editing an attribute in the
|
||||
* dialog.
|
||||
*
|
||||
* @return array
|
||||
* The form element.
|
||||
*/
|
||||
public function buildFormElement($default_value);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\AttributeManager.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Cache\CacheBackendInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Plugin\DefaultPluginManager;
|
||||
|
||||
/**
|
||||
* Manages attributes.
|
||||
*/
|
||||
class AttributeManager extends DefaultPluginManager {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
|
||||
parent::__construct('Plugin/Linkit/Attribute', $namespaces, $module_handler, 'Drupal\linkit\AttributeInterface', 'Drupal\linkit\Annotation\Attribute');
|
||||
|
||||
$this->alterInfo('linkit_attribute');
|
||||
$this->setCacheBackend($cache_backend, 'linkit_attributes');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\ConfigurableAttributeBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
/**
|
||||
* Provides a base class for configurable attributes.
|
||||
*
|
||||
* @see plugin_api
|
||||
*/
|
||||
abstract class ConfigurableAttributeBase extends AttributeBase implements ConfigurableAttributeInterface {
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\ConfigurableAttributeInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Plugin\PluginFormInterface;
|
||||
|
||||
/**
|
||||
* Defines the interface for configurable attributes.
|
||||
*
|
||||
* @see \Drupal\linkit\Annotation\Attribute
|
||||
* @see \Drupal\linkit\ConfigurableAttributeBase
|
||||
* @see \Drupal\linkit\AttributeInterface
|
||||
* @see \Drupal\linkit\AttributeBase
|
||||
* @see \Drupal\linkit\AttributeManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
interface ConfigurableAttributeInterface extends AttributeInterface, PluginFormInterface {
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\ConfigurableMatcherBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
/**
|
||||
* Provides a base class for configurable matchers.
|
||||
*
|
||||
* @see plugin_api
|
||||
*/
|
||||
abstract class ConfigurableMatcherBase extends MatcherBase implements ConfigurableMatcherInterface {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\ConfigurableMatcherInterface.
|
||||
*/
|
||||
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Plugin\PluginFormInterface;
|
||||
|
||||
/**
|
||||
* Defines the interface for configurable matchers.
|
||||
*
|
||||
* @see \Drupal\linkit\Annotation\Matcher
|
||||
* @see \Drupal\linkit\ConfigurableMatcherBase
|
||||
* @see \Drupal\linkit\MatcherInterface
|
||||
* @see \Drupal\linkit\MatcherBase
|
||||
* @see \Drupal\linkit\MatcherManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
interface ConfigurableMatcherInterface extends MatcherInterface, PluginFormInterface {
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Controller\AutocompleteController.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Controller;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\linkit\ResultManager;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class AutocompleteController implements ContainerInjectionInterface {
|
||||
|
||||
/**
|
||||
* The linkit profile storage service.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $linkitProfileStorage;
|
||||
|
||||
/**
|
||||
* The result manager.
|
||||
*
|
||||
* @var \Drupal\linkit\ResultManager
|
||||
*/
|
||||
protected $resultManager;
|
||||
|
||||
/**
|
||||
* The linkit profile.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* Constructs a EntityAutocompleteController object.
|
||||
*
|
||||
* @param ResultManager $resultManager
|
||||
* The result service.
|
||||
* @param \Drupal\Core\Entity\EntityStorageInterface $linkit_profile_storage
|
||||
* The linkit profile storage service.
|
||||
*/
|
||||
public function __construct(EntityStorageInterface $linkit_profile_storage, ResultManager $resultManager) {
|
||||
$this->linkitProfileStorage = $linkit_profile_storage;
|
||||
$this->resultManager = $resultManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('entity.manager')->getStorage('linkit_profile'),
|
||||
$container->get('linkit.result_manager')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Menu callback for linkit search autocompletion.
|
||||
*
|
||||
* Like other autocomplete functions, this function inspects the 'q' query
|
||||
* parameter for the string to use to search for suggestions.
|
||||
*
|
||||
* @param Request $request
|
||||
* The request.
|
||||
* @param $linkit_profile_id
|
||||
* The linkit profile id.
|
||||
* @return JsonResponse
|
||||
* A JSON response containing the autocomplete suggestions.
|
||||
*/
|
||||
public function autocomplete(Request $request, $linkit_profile_id) {
|
||||
$this->linkitProfile = $this->linkitProfileStorage->load($linkit_profile_id);
|
||||
$string = Unicode::strtolower($request->query->get('q'));
|
||||
|
||||
$matches = $this->resultManager->getResults($this->linkitProfile, $string);
|
||||
|
||||
$json_object = new \stdClass();
|
||||
$json_object->matches = $matches;
|
||||
|
||||
return new JsonResponse($json_object);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Controller\LinkitController.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
|
||||
/**
|
||||
* Provides route responses for linkit.module.
|
||||
*/
|
||||
class LinkitController extends ControllerBase {
|
||||
|
||||
/**
|
||||
* Route title callback.
|
||||
*
|
||||
* @param \Drupal\linkit\ProfileInterface $linkit_profile
|
||||
* The profile.
|
||||
*
|
||||
* @return string
|
||||
* The profile label as a render array.
|
||||
*/
|
||||
public function profileTitle(ProfileInterface $linkit_profile) {
|
||||
return $this->t('Edit %label profile', array('%label' => $linkit_profile->label()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Route title callback.
|
||||
*
|
||||
* @param \Drupal\linkit\ProfileInterface $linkit_profile
|
||||
* The profile.
|
||||
* @param string $plugin_instance_id
|
||||
* The plugin instance id.
|
||||
*
|
||||
* @return string
|
||||
* The title for the matcher edit form.
|
||||
*/
|
||||
public function matcherTitle(ProfileInterface $linkit_profile, $plugin_instance_id) {
|
||||
/** @var \Drupal\linkit\MatcherInterface $matcher */
|
||||
$matcher = $linkit_profile->getMatcher($plugin_instance_id);
|
||||
return $this->t('Edit %label matcher', array('%label' => $matcher->getLabel()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Route title callback.
|
||||
*
|
||||
* @param \Drupal\linkit\ProfileInterface $linkit_profile
|
||||
* The profile.
|
||||
* @param string $plugin_instance_id
|
||||
* The plugin instance id.
|
||||
*
|
||||
* @return string
|
||||
* The title for the attribute edit form.
|
||||
*/
|
||||
public function attributeTitle(ProfileInterface $linkit_profile, $plugin_instance_id) {
|
||||
/** @var \Drupal\linkit\AttributeInterface $attribute */
|
||||
$attribute = $linkit_profile->getAttribute($plugin_instance_id);
|
||||
return $this->t('Edit %label attribute', array('%label' => $attribute->getLabel()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Element\Linkit.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Element;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Render\BubbleableMetadata;
|
||||
use Drupal\Core\Render\Element;
|
||||
use Drupal\Core\Render\Element\FormElement;
|
||||
use Drupal\Core\Render\Element\Textfield;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Provides a form element for linkit.
|
||||
*
|
||||
* @FormElement("linkit")
|
||||
*/
|
||||
class Linkit extends FormElement {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getInfo() {
|
||||
$class = get_class($this);
|
||||
return array(
|
||||
'#input' => TRUE,
|
||||
'#size' => 60,
|
||||
'#process' => array(
|
||||
array($class, 'processLinkitAutocomplete'),
|
||||
array($class, 'processGroup'),
|
||||
),
|
||||
'#pre_render' => array(
|
||||
array($class, 'preRenderLinkitElement'),
|
||||
array($class, 'preRenderGroup'),
|
||||
),
|
||||
'#theme' => 'input__textfield',
|
||||
'#theme_wrappers' => array('form_element'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
|
||||
return Textfield::valueCallback($element, $input, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds linkit custom autocomplete functionality to elements.
|
||||
*
|
||||
* Instead of using the core autocomplete, we use our own.
|
||||
*
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \Drupal\Core\Render\Element\FormElement::processAutocomplete
|
||||
*/
|
||||
public static function processLinkitAutocomplete(&$element, FormStateInterface $form_state, &$complete_form) {
|
||||
$url = NULL;
|
||||
$access = FALSE;
|
||||
|
||||
if (!empty($element['#autocomplete_route_name'])) {
|
||||
$parameters = isset($element['#autocomplete_route_parameters']) ? $element['#autocomplete_route_parameters'] : array();
|
||||
$url = Url::fromRoute($element['#autocomplete_route_name'], $parameters)->toString(TRUE);
|
||||
/** @var \Drupal\Core\Access\AccessManagerInterface $access_manager */
|
||||
$access_manager = \Drupal::service('access_manager');
|
||||
$access = $access_manager->checkNamedRoute($element['#autocomplete_route_name'], $parameters, \Drupal::currentUser(), TRUE);
|
||||
}
|
||||
|
||||
if ($access) {
|
||||
$metadata = BubbleableMetadata::createFromRenderArray($element);
|
||||
if ($access->isAllowed()) {
|
||||
$element['#attributes']['class'][] = 'form-linkit-autocomplete';
|
||||
$metadata->addAttachments(['library' => ['linkit/linkit.autocomplete']]);
|
||||
// Provide a data attribute for the JavaScript behavior to bind to.
|
||||
$element['#attributes']['data-autocomplete-path'] = $url->getGeneratedUrl();
|
||||
$metadata = $metadata->merge($url);
|
||||
}
|
||||
$metadata
|
||||
->merge(BubbleableMetadata::createFromObject($access))
|
||||
->applyTo($element);
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares a #type 'linkit' render element for input.html.twig.
|
||||
*
|
||||
* @param array $element
|
||||
* An associative array containing the properties of the element.
|
||||
* Properties used: #title, #value, #description, #size, #attributes.
|
||||
*
|
||||
* @return array
|
||||
* The $element with prepared variables ready for input.html.twig.
|
||||
*/
|
||||
public static function preRenderLinkitElement($element) {
|
||||
$element['#attributes']['type'] = 'text';
|
||||
Element::setAttributes($element, array('id', 'name', 'value', 'size'));
|
||||
static::setAttributes($element, array('form-text'));
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Entity\Profile.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Entity;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
|
||||
use Drupal\linkit\AttributeCollection;
|
||||
use Drupal\linkit\MatcherCollection;
|
||||
use Drupal\linkit\MatcherInterface;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
|
||||
/**
|
||||
* Defines the linkit profile entity.
|
||||
*
|
||||
* @ConfigEntityType(
|
||||
* id = "linkit_profile",
|
||||
* label = @Translation("Linkit profile"),
|
||||
* handlers = {
|
||||
* "list_builder" = "Drupal\linkit\ProfileListBuilder",
|
||||
* "form" = {
|
||||
* "add" = "Drupal\linkit\Form\Profile\AddForm",
|
||||
* "edit" = "Drupal\linkit\Form\Profile\EditForm",
|
||||
* "delete" = "Drupal\Core\Entity\EntityDeleteForm"
|
||||
* }
|
||||
* },
|
||||
* admin_permission = "administer linkit profiles",
|
||||
* config_prefix = "linkit_profile",
|
||||
* entity_keys = {
|
||||
* "id" = "id",
|
||||
* "label" = "label"
|
||||
* },
|
||||
* links = {
|
||||
* "collection" = "/admin/config/content/linkit",
|
||||
* "edit-form" = "/admin/config/content/linkit/manage/{linkit_profile}",
|
||||
* "delete-form" = "/admin/config/content/linkit/manage/{linkit_profile}/delete"
|
||||
* },
|
||||
* config_export = {
|
||||
* "id",
|
||||
* "label",
|
||||
* "description",
|
||||
* "attributes",
|
||||
* "matchers"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class Profile extends ConfigEntityBase implements ProfileInterface, EntityWithPluginCollectionInterface {
|
||||
|
||||
/**
|
||||
* The ID of this profile.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* The human-readable label of this profile.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $label;
|
||||
|
||||
/**
|
||||
* Description of this profile.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* Configured attribute for this profile.
|
||||
*
|
||||
* An associative array of attribute assigned to the profile, keyed by the
|
||||
* attribute id of each attribute and using the properties:
|
||||
* - id: The plugin ID of the attribute instance.
|
||||
* - status: (optional) A Boolean indicating whether the attribute is enabled
|
||||
* in the profile. Defaults to FALSE.
|
||||
* - weight: (optional) The weight of the attribute in the profile.
|
||||
* Defaults to 0.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [];
|
||||
|
||||
/**
|
||||
* Holds the collection of attributes that are attached to this profile.
|
||||
*
|
||||
* @var \Drupal\linkit\AttributeCollection
|
||||
*/
|
||||
protected $attributeCollection;
|
||||
|
||||
/**
|
||||
* Configured matchers for this profile.
|
||||
*
|
||||
* An associative array of matchers assigned to the profile, keyed by the
|
||||
* matcher ID of each matcher and using the properties:
|
||||
* - id: The plugin ID of the matchers instance.
|
||||
* - status: (optional) A Boolean indicating whether the matchers is enabled
|
||||
* in the profile. Defaults to FALSE.
|
||||
* - weight: (optional) The weight of the matchers in the profile.
|
||||
* Defaults to 0.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $matchers = [];
|
||||
|
||||
/**
|
||||
* Holds the collection of matchers that are attached to this profile.
|
||||
*
|
||||
* @var \Drupal\linkit\MatcherCollection
|
||||
*/
|
||||
protected $matcherCollection;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDescription() {
|
||||
return $this->get('description');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDescription($description) {
|
||||
$this->set('description', trim($description));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttribute($attribute_id) {
|
||||
return $this->getAttributes()->get($attribute_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes() {
|
||||
if (!$this->attributeCollection) {
|
||||
$this->attributeCollection = new AttributeCollection($this->getAttributeManager(), $this->attributes);
|
||||
$this->attributeCollection->sort();
|
||||
}
|
||||
return $this->attributeCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addAttribute(array $configuration) {
|
||||
$this->getAttributes()->addInstanceId($configuration['id'], $configuration);
|
||||
return $configuration['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeAttribute($attribute_id) {
|
||||
unset($this->attributes[$attribute_id]);
|
||||
$this->getAttributes()->removeInstanceId($attribute_id);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setAttributeConfig($attribute_id, array $configuration) {
|
||||
$this->attributes[$attribute_id] = $configuration;
|
||||
$this->getAttributes()->setInstanceConfiguration($attribute_id, $configuration);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMatcher($instance_id) {
|
||||
return $this->getMatchers()->get($instance_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMatchers() {
|
||||
if (!$this->matcherCollection) {
|
||||
$this->matcherCollection = new MatcherCollection($this->getMatcherManager(), $this->matchers);
|
||||
$this->matcherCollection->sort();
|
||||
}
|
||||
return $this->matcherCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addMatcher(array $configuration) {
|
||||
$configuration['uuid'] = $this->uuidGenerator()->generate();
|
||||
$this->getMatchers()->addInstanceId($configuration['uuid'], $configuration);
|
||||
return $configuration['uuid'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeMatcher(MatcherInterface $matcher) {
|
||||
$this->getMatchers()->removeInstanceId($matcher->getUuid());
|
||||
$this->save();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setMatcherConfig($instance_id, array $configuration) {
|
||||
$this->matchers[$instance_id] = $configuration;
|
||||
$this->getMatchers()->setInstanceConfiguration($instance_id, $configuration);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginCollections() {
|
||||
return array(
|
||||
'attributes' => $this->getAttributes(),
|
||||
'matchers' => $this->getMatchers(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the attribute manager.
|
||||
*
|
||||
* @return \Drupal\Component\Plugin\PluginManagerInterface
|
||||
* The attribute manager.
|
||||
*/
|
||||
protected function getAttributeManager() {
|
||||
return \Drupal::service('plugin.manager.linkit.attribute');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the matcher manager.
|
||||
*
|
||||
* @return \Drupal\Component\Plugin\PluginManagerInterface
|
||||
* The matcher manager.
|
||||
*/
|
||||
protected function getMatcherManager() {
|
||||
return \Drupal::service('plugin.manager.linkit.matcher');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Attribute\AddForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Attribute;
|
||||
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\linkit\AttributeManager;
|
||||
use Drupal\linkit\ConfigurableAttributeInterface;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides a form to apply attributes to a profile.
|
||||
*/
|
||||
class AddForm extends FormBase {
|
||||
|
||||
/**
|
||||
* The profiles to which the attributes will be applied.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* The attribute manager.
|
||||
*
|
||||
* @var \Drupal\linkit\AttributeManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* Constructs a new AddForm.
|
||||
*
|
||||
* @param \Drupal\linkit\AttributeManager $manager
|
||||
* The attribute manager.
|
||||
*/
|
||||
public function __construct(AttributeManager $manager) {
|
||||
$this->manager = $manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('plugin.manager.linkit.attribute')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return "linkit_attribute_add_form";
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
|
||||
$this->linkitProfile = $linkit_profile;
|
||||
|
||||
$form['#attached']['library'][] = 'linkit/linkit.admin';
|
||||
$header = [
|
||||
'label' => $this->t('Attributes'),
|
||||
'description' => $this->t('Description'),
|
||||
];
|
||||
|
||||
$form['plugin'] = [
|
||||
'#type' => 'tableselect',
|
||||
'#header' => $header,
|
||||
'#options' => $this->buildRows(),
|
||||
'#empty' => $this->t('No attributes available.'),
|
||||
'#multiple' => FALSE,
|
||||
];
|
||||
|
||||
$form['actions'] = ['#type' => 'actions'];
|
||||
$form['actions']['submit'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save and continue'),
|
||||
'#submit' => ['::submitForm'],
|
||||
'#tableselect' => TRUE,
|
||||
'#button_type' => 'primary',
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
if (empty($form_state->getValue('plugin'))) {
|
||||
$form_state->setErrorByName('plugin', $this->t('No attribute selected.'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$form_state->cleanValues();
|
||||
|
||||
/** @var \Drupal\linkit\AttributeInterface $plugin */
|
||||
$plugin = $this->manager->createInstance($form_state->getValue('plugin'));
|
||||
$plugin_id = $this->linkitProfile->addAttribute($plugin->getConfiguration());
|
||||
$this->linkitProfile->save();
|
||||
|
||||
$this->logger('linkit')->notice('Added %label attribute to the @profile profile.', [
|
||||
'%label' => $this->linkitProfile->getAttribute($plugin_id)->getLabel(),
|
||||
'@profile' => $this->linkitProfile->label(),
|
||||
]);
|
||||
|
||||
$is_configurable = $plugin instanceof ConfigurableAttributeInterface;
|
||||
if ($is_configurable) {
|
||||
$form_state->setRedirect('linkit.attribute.edit', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $plugin_id,
|
||||
]);
|
||||
}
|
||||
else {
|
||||
drupal_set_message($this->t('Added %label attribute.', ['%label' => $plugin->getLabel()]));
|
||||
|
||||
$form_state->setRedirect('linkit.attributes', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the table rows.
|
||||
*
|
||||
* Only attributes that is not already applied to the profile are shown.
|
||||
*
|
||||
* @return array
|
||||
* An array of table rows.
|
||||
*/
|
||||
private function buildRows() {
|
||||
$rows = [];
|
||||
|
||||
$applied_plugins = $this->linkitProfile->getAttributes()->getConfiguration();
|
||||
$all_plugins = $this->manager->getDefinitions();
|
||||
uasort($all_plugins, function ($a, $b) {
|
||||
return strnatcasecmp($a['label'], $b['label']);
|
||||
});
|
||||
foreach (array_diff_key($all_plugins, $applied_plugins) as $definition) {
|
||||
/** @var \Drupal\linkit\AttributeInterface $plugin */
|
||||
$plugin = $this->manager->createInstance($definition['id']);
|
||||
|
||||
$row = [
|
||||
'label' => (string) $plugin->getLabel(),
|
||||
'description' => (string) $plugin->getDescription(),
|
||||
];
|
||||
|
||||
$rows[$plugin->getPluginId()] = $row;
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Attribute\DeleteForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Attribute;
|
||||
|
||||
use Drupal\Core\Form\ConfirmFormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Provides a form to remove an attribute from a profile.
|
||||
*/
|
||||
class DeleteForm extends ConfirmFormBase {
|
||||
|
||||
/**
|
||||
* The profiles that the attribute is applied to.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* The attribute to be removed from the profile.
|
||||
*
|
||||
* @var \Drupal\linkit\AttributeInterface
|
||||
*/
|
||||
protected $linkitAttribute;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getQuestion() {
|
||||
return $this->t('Are you sure you want to delete the @plugin attribute from the %profile profile?', ['%profile' => $this->linkitProfile->label(), '@plugin' => $this->linkitAttribute->getLabel()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCancelUrl() {
|
||||
return Url::fromRoute('linkit.attributes', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'linkit_attribute_delete_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL, $plugin_instance_id = NULL) {
|
||||
$this->linkitProfile = $linkit_profile;
|
||||
|
||||
if (!$this->linkitProfile->getAttributes()->has($plugin_instance_id)) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
$this->linkitAttribute = $this->linkitProfile->getAttribute($plugin_instance_id);
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
if ($this->linkitProfile->getAttributes()->has($this->linkitAttribute->getPluginId())) {
|
||||
$this->linkitProfile->removeAttribute($this->linkitAttribute->getPluginId());
|
||||
$this->linkitProfile->save();
|
||||
|
||||
drupal_set_message($this->t('The attribute %label has been deleted.', ['%label' => $this->linkitAttribute->getLabel()]));
|
||||
$this->logger('linkit')->notice('The attribute %label has been deleted in the @profile profile.', [
|
||||
'%label' => $this->linkitAttribute->getLabel(),
|
||||
'@profile' => $this->linkitProfile->label(),
|
||||
]);
|
||||
}
|
||||
|
||||
$form_state->setRedirect('linkit.attributes', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Attribute\EditForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Attribute;
|
||||
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
|
||||
/**
|
||||
* Provides an edit form for attributes.
|
||||
*/
|
||||
class EditForm extends FormBase {
|
||||
|
||||
/**
|
||||
* The profiles to which the attributes will be applied.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* The attribute to edit.
|
||||
*
|
||||
* @var \Drupal\linkit\ConfigurableAttributeInterface
|
||||
*/
|
||||
protected $linkitAttribute;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'linkit_attribute_edit_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL, $plugin_instance_id = NULL) {
|
||||
$this->linkitProfile = $linkit_profile;
|
||||
$this->linkitAttribute = $this->linkitProfile->getAttribute($plugin_instance_id);
|
||||
$form['data'] = [
|
||||
'#tree' => true,
|
||||
];
|
||||
|
||||
$form['data'] += $this->linkitAttribute->buildConfigurationForm($form, $form_state);
|
||||
|
||||
$form['actions'] = array('#type' => 'actions');
|
||||
$form['actions']['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save changes'),
|
||||
'#submit' => array('::submitForm'),
|
||||
'#button_type' => 'primary',
|
||||
);
|
||||
$form['actions']['delete'] = array(
|
||||
'#type' => 'link',
|
||||
'#title' => $this->t('Delete'),
|
||||
'#url' => Url::fromRoute('linkit.attribute.delete', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $this->linkitAttribute->getPluginId(),
|
||||
]),
|
||||
'#attributes' => [
|
||||
'class' => ['button', 'button--danger'],
|
||||
],
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$form_state->cleanValues();
|
||||
$plugin_data = (new FormState())->setValues($form_state->getValue('data'));
|
||||
$this->linkitAttribute->submitConfigurationForm($form, $plugin_data);
|
||||
$this->linkitProfile->save();
|
||||
|
||||
drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->linkitAttribute->getLabel())));
|
||||
$this->logger('linkit')->notice('The attribute %label has been updated in the @profile profile.', [
|
||||
'%label' => $this->linkitAttribute->getLabel(),
|
||||
'@profile' => $this->linkitProfile->label(),
|
||||
]);
|
||||
|
||||
$form_state->setRedirect('linkit.attributes', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Attribute\OverviewForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Attribute;
|
||||
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\AttributeManager;
|
||||
use Drupal\linkit\ConfigurableAttributeInterface;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides an overview form for attribute on a profile.
|
||||
*/
|
||||
class OverviewForm extends FormBase {
|
||||
|
||||
/**
|
||||
* The profiles to which the attributes are applied to.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
private $linkitProfile;
|
||||
|
||||
/**
|
||||
* The attribute manager.
|
||||
*
|
||||
* @var \Drupal\linkit\AttributeManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* Constructs a new OverviewForm.
|
||||
*
|
||||
* @param \Drupal\linkit\AttributeManager $manager
|
||||
* The attribute manager.
|
||||
*/
|
||||
public function __construct(AttributeManager $manager) {
|
||||
$this->manager = $manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('plugin.manager.linkit.attribute')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return "linkit_attribute_overview_form";
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
|
||||
$this->linkitProfile = $linkit_profile;
|
||||
|
||||
$form['plugins'] = [
|
||||
'#type' => 'table',
|
||||
'#header' => [
|
||||
$this->t('Attribute'),
|
||||
$this->t('Description'),
|
||||
$this->t('Weight'),
|
||||
$this->t('Operations'),
|
||||
],
|
||||
'#empty' => $this->t('No attributes added.'),
|
||||
'#tabledrag' => [
|
||||
[
|
||||
'action' => 'order',
|
||||
'relationship' => 'sibling',
|
||||
'group' => 'plugin-order-weight',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($this->linkitProfile->getAttributes() as $plugin) {
|
||||
$key = $plugin->getPluginId();
|
||||
|
||||
$form['plugins'][$key]['#attributes']['class'][] = 'draggable';
|
||||
$form['plugins'][$key]['#weight'] = $plugin->getWeight();
|
||||
|
||||
$form['plugins'][$key]['label'] = [
|
||||
'#plain_text' => (string) $plugin->getLabel(),
|
||||
];
|
||||
|
||||
$form['plugins'][$key]['description'] = [
|
||||
'#plain_text' => (string) $plugin->getDescription(),
|
||||
];
|
||||
|
||||
$form['plugins'][$key]['weight'] = [
|
||||
'#type' => 'weight',
|
||||
'#title' => t('Weight for @title', ['@title' => (string) $plugin->getLabel()]),
|
||||
'#title_display' => 'invisible',
|
||||
'#default_value' => $plugin->getWeight(),
|
||||
'#attributes' => ['class' => ['plugin-order-weight']],
|
||||
];
|
||||
|
||||
$form['plugins'][$key]['operations'] = [
|
||||
'#type' => 'operations',
|
||||
'#links' => [],
|
||||
];
|
||||
|
||||
$is_configurable = $plugin instanceof ConfigurableAttributeInterface;
|
||||
if ($is_configurable) {
|
||||
$form['plugins'][$key]['operations']['#links']['edit'] = [
|
||||
'title' => t('Edit'),
|
||||
'url' => Url::fromRoute('linkit.attribute.edit', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $key,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
$form['plugins'][$key]['operations']['#links']['delete'] = [
|
||||
'title' => t('Delete'),
|
||||
'url' => Url::fromRoute('linkit.attribute.delete', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $key,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
$form['actions'] = ['#type' => 'actions'];
|
||||
$form['actions']['submit'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save'),
|
||||
'#button_type' => 'primary',
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
foreach ($form_state->getValue('plugins') as $id => $plugin_data) {
|
||||
if ($this->linkitProfile->getAttributes()->has($id)) {
|
||||
$this->linkitProfile->getAttribute($id)->setWeight($plugin_data['weight']);
|
||||
}
|
||||
}
|
||||
$this->linkitProfile->save();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\LinkitEditorDialog.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form;
|
||||
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\filter\Entity\FilterFormat;
|
||||
use Drupal\Core\Ajax\AjaxResponse;
|
||||
use Drupal\Core\Ajax\HtmlCommand;
|
||||
use Drupal\editor\Ajax\EditorDialogSave;
|
||||
use Drupal\Core\Ajax\CloseModalDialogCommand;
|
||||
use Drupal\linkit\AttributeCollection;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides a linkit dialog for text editors.
|
||||
*/
|
||||
class LinkitEditorDialog extends FormBase {
|
||||
|
||||
/**
|
||||
* The editor storage service.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $editorStorage;
|
||||
|
||||
/**
|
||||
* The linkit profile storage service.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $linkitProfileStorage;
|
||||
|
||||
/**
|
||||
* The linkit profile.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* Constructs a form object for linkit dialog.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityStorageInterface $editor_storage
|
||||
* The editor storage service.
|
||||
* @param \Drupal\Core\Entity\EntityStorageInterface $linkit_profile_storage
|
||||
* The linkit profile storage service.
|
||||
*/
|
||||
public function __construct(EntityStorageInterface $editor_storage, EntityStorageInterface $linkit_profile_storage) {
|
||||
$this->editorStorage = $editor_storage;
|
||||
$this->linkitProfileStorage = $linkit_profile_storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('entity.manager')->getStorage('editor'),
|
||||
$container->get('entity.manager')->getStorage('linkit_profile')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'linkit_editor_dialog_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param \Drupal\filter\Entity\FilterFormat $filter_format
|
||||
* The filter format for which this dialog corresponds.
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {
|
||||
// The default values are set directly from \Drupal::request()->request,
|
||||
// provided by the editor plugin opening the dialog.
|
||||
$user_input = $form_state->getUserInput();
|
||||
$input = isset($user_input['editor_object']) ? $user_input['editor_object'] : [];
|
||||
|
||||
/** @var \Drupal\editor\EditorInterface $editor */
|
||||
$editor = $this->editorStorage->load($filter_format->id());
|
||||
$linkit_profile_id = $editor->getSettings()['plugins']['linkit']['linkit_profile'];
|
||||
$this->linkitProfile = $this->linkitProfileStorage->load($linkit_profile_id);
|
||||
|
||||
$form['#tree'] = TRUE;
|
||||
$form['#attached']['library'][] = 'editor/drupal.editor.dialog';
|
||||
$form['#prefix'] = '<div id="linkit-editor-dialog-form">';
|
||||
$form['#suffix'] = '</div>';
|
||||
|
||||
// Everything under the "attributes" key is merged directly into the
|
||||
// generated link tag's attributes.
|
||||
$form['attributes']['href'] = [
|
||||
'#title' => $this->t('Link'),
|
||||
'#type' => 'linkit',
|
||||
'#default_value' => isset($input['href']) ? $input['href'] : '',
|
||||
'#description' => $this->t('Start typing to find content or paste a URL.'),
|
||||
'#autocomplete_route_name' => 'linkit.autocomplete',
|
||||
'#autocomplete_route_parameters' => [
|
||||
'linkit_profile_id' => $linkit_profile_id
|
||||
],
|
||||
'#weight' => 0,
|
||||
];
|
||||
|
||||
$this->addAttributes($form, $form_state, $this->linkitProfile->getAttributes(), $input);
|
||||
|
||||
$form['actions'] = [
|
||||
'#type' => 'actions',
|
||||
];
|
||||
|
||||
$form['actions']['save_modal'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save'),
|
||||
'#submit' => [],
|
||||
'#ajax' => [
|
||||
'callback' => '::submitForm',
|
||||
'event' => 'click',
|
||||
],
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
$attributes = array_filter($form_state->getValue('attributes'));
|
||||
$form_state->setValue('attributes', $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$response = new AjaxResponse();
|
||||
|
||||
if ($form_state->getErrors()) {
|
||||
unset($form['#prefix'], $form['#suffix']);
|
||||
$form['status_messages'] = [
|
||||
'#type' => 'status_messages',
|
||||
'#weight' => -10,
|
||||
];
|
||||
$response->addCommand(new HtmlCommand('#linkit-editor-dialog-form', $form));
|
||||
}
|
||||
else {
|
||||
$response->addCommand(new EditorDialogSave($form_state->getValues()));
|
||||
$response->addCommand(new CloseModalDialogCommand());
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the attributes enabled on the current profile.
|
||||
*
|
||||
* @param array $form
|
||||
* An associative array containing the structure of the form.
|
||||
* @param \Drupal\Core\Form\FormStateInterface $form_state
|
||||
* The current state of the form.
|
||||
* @param AttributeCollection $attributes
|
||||
* A collection of attributes for the current profile.
|
||||
* @param array $input
|
||||
* An array with the attribute values from the editor.
|
||||
*/
|
||||
private function addAttributes(array &$form, FormStateInterface &$form_state, AttributeCollection $attributes, array $input) {
|
||||
if ($attributes->count()) {
|
||||
$form['linkit_attributes'] = [
|
||||
'#type' => 'container',
|
||||
'#title' => $this->t('Attributes'),
|
||||
'#weight' => '10',
|
||||
];
|
||||
|
||||
/** @var \Drupal\linkit\AttributeInterface $plugin */
|
||||
foreach ($attributes as $plugin) {
|
||||
$plugin_name = $plugin->getHtmlName();
|
||||
|
||||
$default_value = isset($input[$plugin_name]) ? $input[$plugin_name] : '';
|
||||
$form['linkit_attributes'][$plugin_name] = $plugin->buildFormElement($default_value);
|
||||
$form['linkit_attributes'][$plugin_name] += [
|
||||
'#parents' => [
|
||||
'attributes', $plugin_name,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the linkit profile entity.
|
||||
*
|
||||
* @return \Drupal\linkit\ProfileInterface
|
||||
* The current linkit profile used by this form.
|
||||
*/
|
||||
public function getLinkitProfile() {
|
||||
return $this->linkitProfile;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Matcher\AddForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Matcher;
|
||||
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\linkit\ConfigurableMatcherInterface;
|
||||
use Drupal\linkit\MatcherManager;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides a form to apply matchers to a profile.
|
||||
*/
|
||||
class AddForm extends FormBase {
|
||||
|
||||
/**
|
||||
* The profiles to which the matchers will be applied.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
|
||||
/**
|
||||
* The matcher manager.
|
||||
*
|
||||
* @var \Drupal\linkit\MatcherManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* Constructs a new AddForm.
|
||||
*
|
||||
* @param \Drupal\linkit\MatcherManager $manager
|
||||
* The matcher manager.
|
||||
*/
|
||||
public function __construct(MatcherManager $manager) {
|
||||
$this->manager = $manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('plugin.manager.linkit.matcher')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return "linkit_matcher_add_form";
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
|
||||
$this->linkitProfile = $linkit_profile;
|
||||
|
||||
$form['#attached']['library'][] = 'linkit/linkit.admin';
|
||||
$header = [
|
||||
'label' => $this->t('Matchers'),
|
||||
];
|
||||
|
||||
$form['plugin'] = [
|
||||
'#type' => 'tableselect',
|
||||
'#header' => $header,
|
||||
'#options' => $this->buildRows(),
|
||||
'#empty' => $this->t('No matchers available.'),
|
||||
'#multiple' => FALSE,
|
||||
];
|
||||
|
||||
$form['actions'] = ['#type' => 'actions'];
|
||||
$form['actions']['submit'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save and continue'),
|
||||
'#submit' => ['::submitForm'],
|
||||
'#tableselect' => TRUE,
|
||||
'#button_type' => 'primary',
|
||||
];
|
||||
|
||||
$options = [];
|
||||
foreach ($this->manager->getDefinitions() as $id => $plugin) {
|
||||
$options[$id] = $plugin['label'];
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
if (empty($form_state->getValue('plugin'))) {
|
||||
$form_state->setErrorByName('plugin', $this->t('No matcher selected.'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$form_state->cleanValues();
|
||||
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance($form_state->getValue('plugin'));
|
||||
|
||||
$plugin_uuid = $this->linkitProfile->addMatcher($plugin->getConfiguration());
|
||||
$this->linkitProfile->save();
|
||||
|
||||
$this->logger('linkit')->notice('Added %label matcher to the @profile profile.', [
|
||||
'%label' => $this->linkitProfile->getMatcher($plugin_uuid)->getLabel(),
|
||||
'@profile' => $this->linkitProfile->label(),
|
||||
]);
|
||||
|
||||
$is_configurable = $plugin instanceof ConfigurableMatcherInterface;
|
||||
if ($is_configurable) {
|
||||
$form_state->setRedirect('linkit.matcher.edit', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $plugin_uuid,
|
||||
]);
|
||||
}
|
||||
else {
|
||||
drupal_set_message($this->t('Added %label matcher.', ['%label' => $plugin->getLabel()]));
|
||||
|
||||
$form_state->setRedirect('linkit.matchers', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the table rows.
|
||||
*
|
||||
* @return array
|
||||
* An array of table rows.
|
||||
*/
|
||||
private function buildRows() {
|
||||
$rows = [];
|
||||
$all_plugins = $this->manager->getDefinitions();
|
||||
uasort($all_plugins, function ($a, $b) {
|
||||
return strnatcasecmp($a['label'], $b['label']);
|
||||
});
|
||||
foreach ($all_plugins as $definition) {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance($definition['id']);
|
||||
$row = [
|
||||
'label' => $plugin->getLabel(),
|
||||
];
|
||||
$rows[$plugin->getPluginId()] = $row;
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Matcher\DeleteForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Matcher;
|
||||
|
||||
use Drupal\Core\Form\ConfirmFormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Provides a form to remove a matcher from a profile.
|
||||
*/
|
||||
class DeleteForm extends ConfirmFormBase {
|
||||
|
||||
/**
|
||||
* The profiles that the matcher is applied to.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* The matcher to be removed from the profile.
|
||||
*
|
||||
* @var \Drupal\linkit\MatcherInterface
|
||||
*/
|
||||
protected $linkitMatcher;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getQuestion() {
|
||||
return $this->t('Are you sure you want to delete the @plugin matcher from the %profile profile?', ['%profile' => $this->linkitProfile->label(), '@plugin' => $this->linkitMatcher->getLabel()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCancelUrl() {
|
||||
return Url::fromRoute('linkit.matchers', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'linkit_matcher_delete_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL, $plugin_instance_id = NULL) {
|
||||
$this->linkitProfile = $linkit_profile;
|
||||
|
||||
if (!$this->linkitProfile->getMatchers()->has($plugin_instance_id)) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
$this->linkitMatcher = $this->linkitProfile->getMatcher($plugin_instance_id);
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->linkitProfile->removeMatcher($this->linkitMatcher);
|
||||
|
||||
drupal_set_message($this->t('The matcher %label has been deleted.', ['%label' => $this->linkitMatcher->getLabel()]));
|
||||
$this->logger('linkit')->notice('The matcher %label has been deleted in the @profile profile.', [
|
||||
'%label' => $this->linkitMatcher->getLabel(),
|
||||
'@profile' => $this->linkitProfile->label(),
|
||||
]);
|
||||
|
||||
$form_state->setRedirect('linkit.matchers', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Matcher\EditForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Matcher;
|
||||
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
|
||||
/**
|
||||
* Provides an edit form for matchers.
|
||||
*/
|
||||
class EditForm extends FormBase {
|
||||
|
||||
/**
|
||||
* The profiles to which the matchers will be applied.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* The matcher to edit.
|
||||
*
|
||||
* @var \Drupal\linkit\ConfigurableMatcherInterface
|
||||
*/
|
||||
protected $linkitMatcher;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'linkit_matcher_edit_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL, $plugin_instance_id = NULL) {
|
||||
$this->linkitProfile = $linkit_profile;
|
||||
$this->linkitMatcher = $this->linkitProfile->getMatcher($plugin_instance_id);
|
||||
|
||||
$form += $this->linkitMatcher->buildConfigurationForm($form, $form_state);
|
||||
|
||||
$form['actions'] = array('#type' => 'actions');
|
||||
$form['actions']['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save changes'),
|
||||
'#submit' => array('::submitForm'),
|
||||
'#button_type' => 'primary',
|
||||
);
|
||||
$form['actions']['delete'] = array(
|
||||
'#type' => 'link',
|
||||
'#title' => $this->t('Delete'),
|
||||
'#url' => Url::fromRoute('linkit.matcher.delete', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $this->linkitMatcher->getUuid(),
|
||||
]),
|
||||
'#attributes' => [
|
||||
'class' => ['button', 'button--danger'],
|
||||
],
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$form_state->cleanValues();
|
||||
$plugin_data = (new FormState())->setValues($form_state->getValues());
|
||||
$this->linkitMatcher->submitConfigurationForm($form, $plugin_data);
|
||||
$this->linkitProfile->save();
|
||||
|
||||
drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->linkitMatcher->getLabel())));
|
||||
$this->logger('linkit')->notice('The matcher %label has been updated in the @profile profile.', [
|
||||
'%label' => $this->linkitMatcher->getLabel(),
|
||||
'@profile' => $this->linkitProfile->label(),
|
||||
]);
|
||||
|
||||
$form_state->setRedirect('linkit.matchers', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Matcher\OverviewForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Matcher;
|
||||
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\ConfigurableMatcherInterface;
|
||||
use Drupal\linkit\MatcherManager;
|
||||
use Drupal\linkit\ProfileInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides an overview form for matchers on a profile.
|
||||
*/
|
||||
class OverviewForm extends FormBase {
|
||||
|
||||
/**
|
||||
* The profiles to which the matchers are applied to.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
private $linkitProfile;
|
||||
|
||||
/**
|
||||
* The matcher manager.
|
||||
*
|
||||
* @var \Drupal\linkit\MatcherManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* Constructs a new OverviewForm.
|
||||
*
|
||||
* @param \Drupal\linkit\MatcherManager $manager
|
||||
* The matcher manager.
|
||||
*/
|
||||
public function __construct(MatcherManager $manager) {
|
||||
$this->manager = $manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('plugin.manager.linkit.matcher')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return "linkit_matcher_overview_form";
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
|
||||
$this->linkitProfile = $linkit_profile;
|
||||
$form['#attached']['library'][] = 'linkit/linkit.admin';
|
||||
$form['plugins'] = [
|
||||
'#type' => 'table',
|
||||
'#header' => [
|
||||
[
|
||||
'data' => $this->t('Matcher'),
|
||||
'colspan' => 2
|
||||
],
|
||||
$this->t('Weight'),
|
||||
$this->t('Operations'),
|
||||
],
|
||||
'#empty' => $this->t('No matchers added.'),
|
||||
'#tabledrag' => [
|
||||
[
|
||||
'action' => 'order',
|
||||
'relationship' => 'sibling',
|
||||
'group' => 'plugin-order-weight',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($this->linkitProfile->getMatchers() as $plugin) {
|
||||
$key = $plugin->getUuid();
|
||||
|
||||
$form['plugins'][$key]['#attributes']['class'][] = 'draggable';
|
||||
$form['plugins'][$key]['#weight'] = $plugin->getWeight();
|
||||
|
||||
$form['plugins'][$key]['label'] = [
|
||||
'#plain_text' => (string) $plugin->getLabel(),
|
||||
];
|
||||
|
||||
$form['plugins'][$key]['summary'] = [];
|
||||
|
||||
$summary = $plugin->getSummary();
|
||||
if (!empty($summary)) {
|
||||
$form['plugins'][$key]['summary'] = [
|
||||
'#type' => 'inline_template',
|
||||
'#template' => '<div class="linkit-plugin-summary">{{ summary|safe_join("<br />") }}</div>',
|
||||
'#context' => ['summary' => $summary],
|
||||
];
|
||||
}
|
||||
|
||||
$form['plugins'][$key]['weight'] = [
|
||||
'#type' => 'weight',
|
||||
'#title' => t('Weight for @title', ['@title' => (string) $plugin->getLabel()]),
|
||||
'#title_display' => 'invisible',
|
||||
'#default_value' => $plugin->getWeight(),
|
||||
'#attributes' => ['class' => ['plugin-order-weight']],
|
||||
];
|
||||
|
||||
$form['plugins'][$key]['operations'] = [
|
||||
'#type' => 'operations',
|
||||
'#links' => [],
|
||||
];
|
||||
|
||||
$is_configurable = $plugin instanceof ConfigurableMatcherInterface;
|
||||
if ($is_configurable) {
|
||||
$form['plugins'][$key]['operations']['#links']['edit'] = [
|
||||
'title' => t('Edit'),
|
||||
'url' => Url::fromRoute('linkit.matcher.edit', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $key,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
$form['plugins'][$key]['operations']['#links']['delete'] = [
|
||||
'title' => t('Delete'),
|
||||
'url' => Url::fromRoute('linkit.matcher.delete', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $key,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
$form['actions'] = ['#type' => 'actions'];
|
||||
$form['actions']['submit'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Save'),
|
||||
'#button_type' => 'primary',
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
foreach ($form_state->getValue('plugins') as $id => $plugin_data) {
|
||||
if ($this->linkitProfile->getMatchers()->has($id)) {
|
||||
$this->linkitProfile->getMatcher($id)->setWeight($plugin_data['weight']);
|
||||
}
|
||||
}
|
||||
$this->linkitProfile->save();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Profile\AddForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Profile;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Controller for profile addition forms.
|
||||
*
|
||||
* @see \Drupal\linkit\Profile\FormBase
|
||||
*/
|
||||
class AddForm extends FormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function actions(array $form, FormStateInterface $form_state) {
|
||||
$actions = parent::actions($form, $form_state);
|
||||
$actions['submit']['#value'] = $this->t('Save and manage matchers');
|
||||
return $actions;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Profile\EditForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Profile;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Provides an edit form for profile.
|
||||
*
|
||||
* @see \Drupal\linkit\Profile\FormBase
|
||||
*/
|
||||
class EditForm extends FormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function actions(array $form, FormStateInterface $form_state) {
|
||||
$actions = parent::actions($form, $form_state);
|
||||
$actions['submit']['#value'] = $this->t('Update profile');
|
||||
$actions['delete']['#value'] = $this->t('Delete profile');
|
||||
return $actions;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Form\Profile\FormBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Form\Profile;
|
||||
|
||||
use Drupal\Core\Entity\EntityForm;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Base form for profile add and edit forms.
|
||||
*/
|
||||
abstract class FormBase extends EntityForm {
|
||||
|
||||
/**
|
||||
* The entity being used by this form.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function form(array $form, FormStateInterface $form_state) {
|
||||
$form['label'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Profile Name'),
|
||||
'#default_value' => $this->entity->label(),
|
||||
'#description' => $this->t('The human-readable name of this profile. This name must be unique.'),
|
||||
'#required' => TRUE,
|
||||
'#size' => 30,
|
||||
];
|
||||
|
||||
$form['id'] = [
|
||||
'#type' => 'machine_name',
|
||||
'#default_value' => $this->entity->id(),
|
||||
'#machine_name' => [
|
||||
'exists' => ['\Drupal\linkit\Entity\Profile', 'load']
|
||||
],
|
||||
'#disabled' => !$this->entity->isNew(),
|
||||
];
|
||||
|
||||
$form['description'] = [
|
||||
'#type' => 'textarea',
|
||||
'#title' => $this->t('Description'),
|
||||
'#default_value' => $this->entity->getDescription(),
|
||||
'#description' => $this->t('The text will be displayed on the <em>profile collection</em> page.'),
|
||||
];
|
||||
|
||||
$form['additional_settings'] = array(
|
||||
'#type' => 'vertical_tabs',
|
||||
'#weight' => 99,
|
||||
);
|
||||
|
||||
return parent::form($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save(array $form, FormStateInterface $form_state) {
|
||||
$linkit_profile = $this->entity;
|
||||
|
||||
// Prevent leading and trailing spaces in linkit profile labels.
|
||||
$linkit_profile->set('label', trim($linkit_profile->label()));
|
||||
|
||||
$status = $linkit_profile->save();
|
||||
$edit_link = $this->entity->link($this->t('Edit'));
|
||||
switch ($status) {
|
||||
case SAVED_NEW:
|
||||
drupal_set_message($this->t('Created new profile %label.', ['%label' => $linkit_profile->label()]));
|
||||
$this->logger('linkit')->notice('Created new profile %label.', ['%label' => $linkit_profile->label(), 'link' => $edit_link]);
|
||||
$form_state->setRedirect('linkit.matchers', [
|
||||
'linkit_profile' => $linkit_profile->id(),
|
||||
]);
|
||||
break;
|
||||
|
||||
case SAVED_UPDATED:
|
||||
drupal_set_message($this->t('Updated profile %label.', ['%label' => $linkit_profile->label()]));
|
||||
$this->logger('linkit')->notice('Updated profile %label.', ['%label' => $linkit_profile->label(), 'link' => $edit_link]);
|
||||
$form_state->setRedirectUrl($linkit_profile->urlInfo('edit-form'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\MatcherBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Plugin\PluginBase;
|
||||
|
||||
/**
|
||||
* Provides a base class for matchers.
|
||||
*
|
||||
* @see plugin_api
|
||||
*/
|
||||
abstract class MatcherBase extends PluginBase implements MatcherInterface, ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The matcher ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $uuid;
|
||||
|
||||
/**
|
||||
* The weight of the matcher compared to others in a matcher collection.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $weight = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
$this->setConfiguration($configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getUuid() {
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLabel() {
|
||||
return $this->pluginDefinition['label'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSummary() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getWeight() {
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setWeight($weight) {
|
||||
return $this->weight = $weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConfiguration() {
|
||||
return [
|
||||
'uuid' => $this->getUuid(),
|
||||
'id' => $this->getPluginId(),
|
||||
'weight' => $this->getWeight(),
|
||||
'settings' => $this->configuration,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setConfiguration(array $configuration) {
|
||||
$configuration += [
|
||||
'uuid' => '',
|
||||
'weight' => '0',
|
||||
'settings' => [],
|
||||
];
|
||||
$this->configuration = $configuration['settings'] + $this->defaultConfiguration();
|
||||
$this->uuid = $configuration['uuid'];
|
||||
$this->weight = $configuration['weight'];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\MatcherCollection.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
|
||||
|
||||
/**
|
||||
* A collection of matchers.
|
||||
*/
|
||||
class MatcherCollection extends DefaultLazyPluginCollection {
|
||||
|
||||
/**
|
||||
* All possible matcher IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $definitions;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return \Drupal\linkit\MatcherInterface
|
||||
*/
|
||||
public function &get($instance_id) {
|
||||
return parent::get($instance_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sortHelper($aID, $bID) {
|
||||
$a_weight = $this->get($aID)->getWeight();
|
||||
$b_weight = $this->get($bID)->getWeight();
|
||||
if ($a_weight == $b_weight) {
|
||||
return strnatcasecmp($this->get($aID)->getLabel(), $this->get($bID)->getLabel());
|
||||
}
|
||||
|
||||
return ($a_weight < $b_weight) ? -1 : 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\MatcherInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Component\Plugin\ConfigurablePluginInterface;
|
||||
use Drupal\Component\Plugin\PluginInspectionInterface;
|
||||
use Drupal\Core\Plugin\PluginFormInterface;
|
||||
|
||||
/**
|
||||
* Defines the interface for matchers.
|
||||
*
|
||||
* @see \Drupal\linkit\Annotation\Matcher
|
||||
* @see \Drupal\linkit\MatcherBase
|
||||
* @see \Drupal\linkit\MatcherManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
interface MatcherInterface extends PluginInspectionInterface, ConfigurablePluginInterface {
|
||||
|
||||
/**
|
||||
* Returns the unique ID representing the matcher.
|
||||
*
|
||||
* @return string
|
||||
* The matcher ID.
|
||||
*/
|
||||
public function getUuid();
|
||||
|
||||
/**
|
||||
* Returns the matcher label.
|
||||
*
|
||||
* @return string
|
||||
* The matcher label.
|
||||
*/
|
||||
public function getLabel();
|
||||
|
||||
/**
|
||||
* Returns the summarized configuration of the matcher.
|
||||
*
|
||||
* @return array
|
||||
* An array of summarized configuration of the matcher.
|
||||
*/
|
||||
public function getSummary();
|
||||
|
||||
/**
|
||||
* Returns the weight of the matcher.
|
||||
*
|
||||
* @return int|string
|
||||
* Either the integer weight of the matcher, or an empty string.
|
||||
*/
|
||||
public function getWeight();
|
||||
|
||||
/**
|
||||
* Sets the weight for the matcher.
|
||||
*
|
||||
* @param int $weight
|
||||
* The weight for this matcher.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setWeight($weight);
|
||||
|
||||
/**
|
||||
* Gets an array with search matches that will be presented in the autocomplete
|
||||
* widget.
|
||||
*
|
||||
* @param $string
|
||||
* The string that contains the text to search for.
|
||||
*
|
||||
* @return array
|
||||
* An array whose values are an associative array containing:
|
||||
* - title: A string to use as the search result label.
|
||||
* - description: (optional) A string with additional information about the
|
||||
* result item.
|
||||
* - path: The URL to the item.
|
||||
* - group: (optional) A string with the group name for the result item.
|
||||
* Best practice is to use the plugin name as group name.
|
||||
*/
|
||||
public function getMatches($string);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\MatcherManager.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Cache\CacheBackendInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Plugin\DefaultPluginManager;
|
||||
|
||||
/**
|
||||
* Manages matchers.
|
||||
*/
|
||||
class MatcherManager extends DefaultPluginManager {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
|
||||
parent::__construct('Plugin/Linkit/Matcher', $namespaces, $module_handler, 'Drupal\linkit\MatcherInterface', 'Drupal\linkit\Annotation\Matcher');
|
||||
|
||||
$this->alterInfo('linkit_matcher');
|
||||
$this->setCacheBackend($cache_backend, 'linkit_matchers');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\MatcherTokensTrait.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
/**
|
||||
* Provides friendly methods for matchers using tokens.
|
||||
*/
|
||||
trait MatcherTokensTrait {
|
||||
|
||||
/**
|
||||
* Inserts a form element with a list of available tokens.
|
||||
*
|
||||
* @param $form
|
||||
* The form array to append the token list to.
|
||||
* @param array $types
|
||||
* An array of token types to use.
|
||||
*/
|
||||
public function insertTokenList(&$form, array $types = array()) {
|
||||
if (\Drupal::moduleHandler()->moduleExists('token')) {
|
||||
// Add the token tree UI.
|
||||
$form['token_tree'] = array(
|
||||
'#theme' => 'token_tree_link',
|
||||
'#token_types' => $types,
|
||||
'#dialog' => TRUE,
|
||||
'#weight' => -90,
|
||||
);
|
||||
}
|
||||
else {
|
||||
$token_items = array();
|
||||
foreach ($this->getAvailableTokens($types) as $type => $tokens) {
|
||||
foreach ($tokens as $name => $info) {
|
||||
$token_description = !empty($info['description']) ? $info['description'] : '';
|
||||
$token_items[$type . ':' . $name] = "[$type:$name]" . ' - ' . $info['name'] . ': ' . $token_description;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($token_items)) {
|
||||
$form['tokens'] = array(
|
||||
'#type' => 'details',
|
||||
'#title' => t('Available tokens'),
|
||||
'#weight' => -90,
|
||||
);
|
||||
|
||||
$form['tokens']['list'] = array(
|
||||
'#theme' => 'item_list',
|
||||
'#items' => $token_items,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all available tokens.
|
||||
*
|
||||
* @param array $types
|
||||
* An array of token types to use.
|
||||
* @return array
|
||||
* An array with available tokens
|
||||
*/
|
||||
public function getAvailableTokens(array $types = array()) {
|
||||
$info = \Drupal::token()->getInfo();
|
||||
$available = array_intersect_key($info['tokens'], array_flip($types));
|
||||
return $available;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\CKEditorPlugin\Linkit.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\CKEditorPlugin;
|
||||
|
||||
use Drupal\ckeditor\CKEditorPluginBase;
|
||||
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\editor\Entity\Editor;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Defines the "linkit" plugin.
|
||||
*
|
||||
* @CKEditorPlugin(
|
||||
* id = "linkit",
|
||||
* label = @Translation("Linkit"),
|
||||
* module = "linkit"
|
||||
* )
|
||||
*/
|
||||
class Linkit extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The Linkit profile storage.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $linkitProfileStorage;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $linkit_profile_storage) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->linkitProfileStorage = $linkit_profile_storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('entity.manager')->getStorage('linkit_profile')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFile() {
|
||||
return drupal_get_path('module', 'linkit') . '/js/plugins/linkit/plugin.js';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConfig(Editor $editor) {
|
||||
return array(
|
||||
'linkit_dialogTitleAdd' => t('Add link'),
|
||||
'linkit_dialogTitleEdit' => t('Edit link'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getButtons() {
|
||||
return array(
|
||||
'Linkit' => array(
|
||||
'label' => t('Linkit'),
|
||||
'image' => drupal_get_path('module', 'linkit') . '/js/plugins/linkit/linkit.png',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
|
||||
$settings = $editor->getSettings();
|
||||
|
||||
$all_profiles = $this->linkitProfileStorage->loadMultiple();
|
||||
|
||||
$options = array();
|
||||
foreach ($all_profiles as $profile) {
|
||||
$options[$profile->id()] = $profile->label();
|
||||
}
|
||||
|
||||
$form['linkit_profile'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Select a linkit profile'),
|
||||
'#options' => $options,
|
||||
'#default_value' => isset($settings['plugins']['linkit']) ? $settings['plugins']['linkit'] : '',
|
||||
'#empty_option' => $this->t('- Select profile -'),
|
||||
'#description' => $this->t('Select the linkit profile you wish to use with this text format.'),
|
||||
'#element_validate' => array(
|
||||
array($this, 'validateLinkitProfileSelection'),
|
||||
),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* #element_validate handler for the "linkit_profile" element in settingsForm().
|
||||
*/
|
||||
public function validateLinkitProfileSelection(array $element, FormStateInterface $form_state) {
|
||||
$toolbar_buttons = $form_state->getValue(array('editor', 'settings', 'toolbar', 'button_groups'));
|
||||
if (strpos($toolbar_buttons, '"Linkit"') !== FALSE && empty($element['#value'])) {
|
||||
$form_state->setError($element, t('Please select the linkit profile you wish to use.'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Derivative\EntityMatcherDeriver.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Derivative;
|
||||
|
||||
use Drupal\Component\Plugin\Derivative\DeriverBase;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* @see plugin_api
|
||||
*/
|
||||
class EntityMatcherDeriver extends DeriverBase implements ContainerDeriverInterface {
|
||||
|
||||
/**
|
||||
* The entity manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* Creates an EntityMatcherDeriver object.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entity_manager) {
|
||||
$this->entityManager = $entity_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, $base_plugin_id) {
|
||||
return new static(
|
||||
$container->get('entity.manager')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDerivativeDefinitions($base_plugin_definition) {
|
||||
foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
|
||||
$has_canonical = $entity_type->hasLinkTemplate('canonical');
|
||||
|
||||
if ($has_canonical) {
|
||||
$this->derivatives[$entity_type_id] = $base_plugin_definition;
|
||||
$this->derivatives[$entity_type_id]['id'] = $base_plugin_definition['id'] . ':' . $entity_type_id;
|
||||
$this->derivatives[$entity_type_id]['label'] = $entity_type->getLabel();
|
||||
$this->derivatives[$entity_type_id]['target_entity'] = $entity_type_id;
|
||||
$this->derivatives[$entity_type_id]['base_plugin_label'] = (string) $base_plugin_definition['label'];
|
||||
}
|
||||
}
|
||||
|
||||
return parent::getDerivativeDefinitions($base_plugin_definition);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Attribute\Accesskey.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Attribute;
|
||||
|
||||
use Drupal\linkit\AttributeBase;
|
||||
|
||||
/**
|
||||
* Accesskey attribute.
|
||||
*
|
||||
* @Attribute(
|
||||
* id = "accesskey",
|
||||
* label = @Translation("Accesskey"),
|
||||
* html_name = "accesskey",
|
||||
* description = @Translation("Basic input field for the accesskey attribute.")
|
||||
* )
|
||||
*/
|
||||
class Accesskey extends AttributeBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildFormElement($default_value) {
|
||||
return [
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Accesskey'),
|
||||
'#default_value' => $default_value,
|
||||
'#maxlength' => 255,
|
||||
'#size' => 40,
|
||||
'#placeholder' => t('The "accesskey" attribute value'),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Attribute\Clazz.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Attribute;
|
||||
|
||||
use Drupal\linkit\AttributeBase;
|
||||
|
||||
/**
|
||||
* Class attribute.
|
||||
*
|
||||
* @TODO: For now Drupal filter_html wont support class attributes with
|
||||
* wildcards.
|
||||
* See: \Drupal\filter\Plugin\Filter\FilterHtml::getHTMLRestrictions
|
||||
* See: core/modules/filter/filter.filter_html.admin.js
|
||||
*
|
||||
* @Attribute(
|
||||
* id = "class",
|
||||
* label = @Translation("Class"),
|
||||
* html_name = "class",
|
||||
* description = @Translation("Basic input field for the class attribute."),
|
||||
* )
|
||||
*/
|
||||
//class Clazz extends AttributeBase {
|
||||
//
|
||||
// /**
|
||||
// * {@inheritdoc}
|
||||
// */
|
||||
// public function buildFormElement($default_value) {
|
||||
// return [
|
||||
// '#type' => 'textfield',
|
||||
// '#title' => t('Class'),
|
||||
// '#maxlength' => 255,
|
||||
// '#size' => 40,
|
||||
// ];
|
||||
// }
|
||||
//
|
||||
//}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Attribute\Id.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Attribute;
|
||||
|
||||
use Drupal\linkit\AttributeBase;
|
||||
|
||||
/**
|
||||
* Id attribute.
|
||||
*
|
||||
* @Attribute(
|
||||
* id = "id",
|
||||
* label = @Translation("Id"),
|
||||
* html_name = "id",
|
||||
* description = @Translation("Basic input field for the id attribute."),
|
||||
* )
|
||||
*/
|
||||
class Id extends AttributeBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildFormElement($default_value) {
|
||||
return [
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Id'),
|
||||
'#default_value' => $default_value,
|
||||
'#maxlength' => 255,
|
||||
'#size' => 40,
|
||||
'#placeholder' => t('The "id" attribute value'),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Attribute\Relationship.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Attribute;
|
||||
|
||||
use Drupal\linkit\AttributeBase;
|
||||
|
||||
/**
|
||||
* Relationship attribute.
|
||||
*
|
||||
* @Attribute(
|
||||
* id = "relationship",
|
||||
* label = @Translation("Relationship"),
|
||||
* html_name = "rel",
|
||||
* description = @Translation("Basic input field for the relationship attribute."),
|
||||
* )
|
||||
*/
|
||||
class Relationship extends AttributeBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildFormElement($default_value) {
|
||||
return [
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Relationship'),
|
||||
'#default_value' => $default_value,
|
||||
'#maxlength' => 255,
|
||||
'#size' => 40,
|
||||
'#placeholder' => t('The "rel" attribute value'),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Attribute\Target.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Attribute;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\linkit\ConfigurableAttributeBase;
|
||||
|
||||
/**
|
||||
* Target attribute.
|
||||
*
|
||||
* @Attribute(
|
||||
* id = "target",
|
||||
* label = @Translation("Target"),
|
||||
* html_name = "target"
|
||||
* )
|
||||
*/
|
||||
class Target extends ConfigurableAttributeBase {
|
||||
|
||||
const SELECT_LIST = 'select_list';
|
||||
const SIMPLE_CHECKBOX = 'simple_checkbox';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildFormElement($default_value) {
|
||||
switch ($this->configuration['widget_type']) {
|
||||
case self::SELECT_LIST:
|
||||
return [
|
||||
'#type' => 'select',
|
||||
'#title' => t('Target'),
|
||||
'#options' => [
|
||||
'' => '',
|
||||
'_blank' => t('New window (_blank)'),
|
||||
'_top' => t('Top window (_top)'),
|
||||
'_self' => t('Same window (_self)'),
|
||||
'_parent' => t('Parent window (_parent)')
|
||||
],
|
||||
'#default_value' => $default_value,
|
||||
];
|
||||
case self::SIMPLE_CHECKBOX:
|
||||
return [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Open in new window'),
|
||||
'#default_value' => $default_value,
|
||||
'#return_value' => '_blank',
|
||||
];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return parent::defaultConfiguration() + [
|
||||
'widget_type' => self::SIMPLE_CHECKBOX,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
$form['widget_type'] = [
|
||||
'#type' => 'radios',
|
||||
'#title' => $this->t('Widget type'),
|
||||
'#default_value' => $this->configuration['widget_type'],
|
||||
'#options' => [
|
||||
self::SELECT_LIST => $this->t('Selectlist with predefined targets.'),
|
||||
self::SIMPLE_CHECKBOX => $this->t('Simple checkbox to allow links to be opened in a new browser window or tab.'),
|
||||
],
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->configuration['widget_type'] = $form_state->getValue('widget_type');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Attribute\Title.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Attribute;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\linkit\ConfigurableAttributeBase;
|
||||
|
||||
/**
|
||||
* Title attribute.
|
||||
*
|
||||
* @Attribute(
|
||||
* id = "title",
|
||||
* label = @Translation("Title"),
|
||||
* html_name = "title",
|
||||
* description = @Translation("Basic input field for the title attribute.")
|
||||
* )
|
||||
*/
|
||||
class Title extends ConfigurableAttributeBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildFormElement($default_value) {
|
||||
$element = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Title'),
|
||||
'#default_value' => $default_value,
|
||||
'#maxlength' => 255,
|
||||
'#size' => 40,
|
||||
'#placeholder' => t('The "title" attribute value'),
|
||||
];
|
||||
|
||||
if ($this->configuration['automatic_title']) {
|
||||
$element['#attached']['library'][] = 'linkit/linkit.attribute.title';
|
||||
$element['#placeholder'] = t('The "title" attribute value (auto populated)');
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return parent::defaultConfiguration() + [
|
||||
'automatic_title' => FALSE,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
$form['automatic_title'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Automatically populate title'),
|
||||
'#default_value' => $this->configuration['automatic_title'],
|
||||
'#description' => $this->t('Automatically populate the title attribute with the title from the match selection.'),
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->configuration['automatic_title'] = $form_state->getValue('automatic_title');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Matcher\EntityMatcher.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Matcher;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Database\Connection;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\linkit\ConfigurableMatcherBase;
|
||||
use Drupal\linkit\MatcherTokensTrait;
|
||||
use Drupal\linkit\Utility\LinkitXss;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @Matcher(
|
||||
* id = "entity",
|
||||
* label = @Translation("Entity"),
|
||||
* deriver = "\Drupal\linkit\Plugin\Derivative\EntityMatcherDeriver"
|
||||
* )
|
||||
*/
|
||||
class EntityMatcher extends ConfigurableMatcherBase {
|
||||
|
||||
use MatcherTokensTrait;
|
||||
|
||||
/**
|
||||
* The database connection.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection
|
||||
*/
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* The entity manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* The module handler service.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
/**
|
||||
* The current user.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $currentUser;
|
||||
|
||||
/**
|
||||
* The target entity type id
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $target_type;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
if (empty($plugin_definition['target_entity'])) {
|
||||
throw new \InvalidArgumentException("Missing required 'target_entity' property for a matcher.");
|
||||
}
|
||||
$this->database = $database;
|
||||
$this->entityManager = $entity_manager;
|
||||
$this->moduleHandler = $module_handler;
|
||||
$this->currentUser = $current_user;
|
||||
$this->target_type = $plugin_definition['target_entity'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('database'),
|
||||
$container->get('entity.manager'),
|
||||
$container->get('module_handler'),
|
||||
$container->get('current_user')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSummary() {
|
||||
$summery = parent::getSummary();
|
||||
$entity_type = $this->entityManager->getDefinition($this->target_type);
|
||||
|
||||
$result_description = $this->configuration['result_description'];
|
||||
if (!empty($result_description)) {
|
||||
$summery[] = $this->t('Result description: @result_description', [
|
||||
'@result_description' => $result_description
|
||||
]);
|
||||
}
|
||||
|
||||
if ($entity_type->hasKey('bundle')) {
|
||||
$has_bundle_filter = !empty($this->configuration['bundles']);
|
||||
$bundles = [];
|
||||
|
||||
if ($has_bundle_filter) {
|
||||
$bundles_info = $this->entityManager->getBundleInfo($this->target_type);
|
||||
foreach ($this->configuration['bundles'] as $bundle) {
|
||||
$bundles[] = $bundles_info[$bundle]['label'];
|
||||
}
|
||||
}
|
||||
|
||||
$summery[] = $this->t('Bundle filter: @bundle_filter', [
|
||||
'@bundle_filter' => $has_bundle_filter ? implode(', ', $bundles) : t('None'),
|
||||
]);
|
||||
|
||||
$summery[] = $this->t('Group by bundle: @bundle_grouping', [
|
||||
'@bundle_grouping' => $this->configuration['group_by_bundle'] ? $this->t('Yes') : $this->t('No'),
|
||||
]);
|
||||
}
|
||||
|
||||
return $summery;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return parent::defaultConfiguration() + [
|
||||
'result_description' => '',
|
||||
'bundles' => [],
|
||||
'group_by_bundle' => FALSE,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
$entity_type = $this->entityManager->getDefinition($this->target_type);
|
||||
$form['result_description'] = [
|
||||
'#title' => $this->t('Result description'),
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => $this->configuration['result_description'],
|
||||
'#size' => 120,
|
||||
'#maxlength' => 255,
|
||||
'#weight' => -100,
|
||||
];
|
||||
|
||||
$this->insertTokenList($form, [$this->target_type]);
|
||||
|
||||
// Filter the possible bundles to use if the entity has bundles.
|
||||
if ($entity_type->hasKey('bundle')) {
|
||||
$bundle_options = [];
|
||||
foreach ($this->entityManager->getBundleInfo($this->target_type) as $bundle_name => $bundle_info) {
|
||||
$bundle_options[$bundle_name] = $bundle_info['label'];
|
||||
}
|
||||
|
||||
$form['bundles'] = [
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => $this->t('Restrict to the selected bundles'),
|
||||
'#options' => $bundle_options,
|
||||
'#default_value' => $this->configuration['bundles'],
|
||||
'#description' => $this->t('If none of the checkboxes is checked, allow all bundles.'),
|
||||
'#element_validate' => [[get_class($this), 'elementValidateFilter']],
|
||||
'#weight' => -50,
|
||||
];
|
||||
|
||||
// Group the results by bundle.
|
||||
$form['group_by_bundle'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Group by bundle'),
|
||||
'#default_value' => $this->configuration['group_by_bundle'],
|
||||
'#weight' => -50,
|
||||
];
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->configuration['result_description'] = $form_state->getValue('result_description');
|
||||
$this->configuration['bundles'] = $form_state->getValue('bundles');
|
||||
$this->configuration['group_by_bundle'] = $form_state->getValue('group_by_bundle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Form element validation handler; Filters the #value property of an element.
|
||||
*/
|
||||
public static function elementValidateFilter(&$element, FormStateInterface $form_state) {
|
||||
$element['#value'] = array_filter($element['#value']);
|
||||
$form_state->setValueForElement($element, $element['#value']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMatches($string) {
|
||||
$query = $this->buildEntityQuery($string);
|
||||
$result = $query->execute();
|
||||
|
||||
if (empty($result)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$matches = [];
|
||||
$entities = $this->entityManager->getStorage($this->target_type)->loadMultiple($result);
|
||||
|
||||
foreach ($entities as $entity_id => $entity) {
|
||||
// Check the access against the defined entity access handler.
|
||||
/** @var \Drupal\Core\Access\AccessResultInterface $access */
|
||||
$access = $entity->access('view', $this->currentUser, TRUE);
|
||||
if (!$access->isAllowed()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$matches[] = [
|
||||
'title' => $this->buildLabel($entity),
|
||||
'description' => $this->buildDescription($entity),
|
||||
'path' => $this->buildPath($entity),
|
||||
'group' => $this->buildGroup($entity),
|
||||
];
|
||||
}
|
||||
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds an EntityQuery to get entities.
|
||||
*
|
||||
* @param $match
|
||||
* Text to match the label against.
|
||||
*
|
||||
* @return \Drupal\Core\Entity\Query\QueryInterface
|
||||
* The EntityQuery object with the basic conditions and sorting applied to
|
||||
* it.
|
||||
*/
|
||||
protected function buildEntityQuery($match) {
|
||||
$match = $this->database->escapeLike($match);
|
||||
|
||||
$entity_type = $this->entityManager->getDefinition($this->target_type);
|
||||
$query = $this->entityManager->getStorage($this->target_type)->getQuery();
|
||||
$label_key = $entity_type->getKey('label');
|
||||
|
||||
if ($label_key) {
|
||||
$query->condition($label_key, '%' . $match . '%', 'LIKE');
|
||||
$query->sort($label_key, 'ASC');
|
||||
}
|
||||
|
||||
// Bundle check.
|
||||
if (!empty($this->configuration['bundles']) && $bundle_key = $entity_type->getKey('bundle')) {
|
||||
$query->condition($bundle_key, $this->configuration['bundles'], 'IN');
|
||||
}
|
||||
|
||||
// Add tags to let other modules alter the query.
|
||||
$query->addTag('linkit_entity_autocomplete');
|
||||
$query->addTag('linkit_entity_' . $this->target_type . '_autocomplete');
|
||||
|
||||
// Add access tag for the query.
|
||||
$query->addTag('entity_access');
|
||||
$query->addTag($this->target_type . '_access');
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the label string used in the match array.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The matched entity.
|
||||
*
|
||||
* @return string
|
||||
* The label for this entity.
|
||||
*/
|
||||
protected function buildLabel($entity) {
|
||||
return Html::escape($entity->label());
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the description string used in the match array.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The matched entity.
|
||||
*
|
||||
* @return string
|
||||
* The description for this entity.
|
||||
*/
|
||||
protected function buildDescription($entity) {
|
||||
$description = \Drupal::token()->replace($this->configuration['result_description'], [$this->target_type => $entity], []);
|
||||
return LinkitXss::descriptionFilter($description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the path string used in the match array.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The matched entity.
|
||||
*
|
||||
* @return string
|
||||
* The URL for this entity.
|
||||
*/
|
||||
protected function buildPath($entity) {
|
||||
return $entity->toUrl()->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the group string used in the match array.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The matched entity.
|
||||
*
|
||||
* @return string
|
||||
* The match group for this entity.
|
||||
*/
|
||||
protected function buildGroup($entity) {
|
||||
$group = $entity->getEntityType()->getLabel();
|
||||
|
||||
// If the entities by this entity should be grouped by bundle, get the
|
||||
// name and append it to the group.
|
||||
if ($this->configuration['group_by_bundle']) {
|
||||
$bundles = $this->entityManager->getBundleInfo($entity->getEntityTypeId());
|
||||
$bundle_label = $bundles[$entity->bundle()]['label'];
|
||||
$group .= ' - ' . $bundle_label;
|
||||
}
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Matcher\FileMatcher.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Matcher;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\image\Entity\ImageStyle;
|
||||
use Drupal\linkit\Utility\LinkitXss;
|
||||
|
||||
/**
|
||||
* @Matcher(
|
||||
* id = "entity:file",
|
||||
* target_entity = "file",
|
||||
* label = @Translation("File"),
|
||||
* provider = "file"
|
||||
* )
|
||||
*/
|
||||
class FileMatcher extends EntityMatcher {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSummary() {
|
||||
$summery = parent::getSummary();
|
||||
|
||||
$summery[] = $this->t('Show image dimensions: @show_image_dimensions', [
|
||||
'@show_image_dimensions' => $this->configuration['images']['show_dimensions'] ? $this->t('Yes') : $this->t('No'),
|
||||
]);
|
||||
|
||||
$summery[] = $this->t('Show image thumbnail: @show_image_thumbnail', [
|
||||
'@show_image_thumbnail' => $this->configuration['images']['show_thumbnail'] ? $this->t('Yes') : $this->t('No'),
|
||||
]);
|
||||
|
||||
if ($this->moduleHandler->moduleExists('image') && $this->configuration['images']['show_thumbnail']) {
|
||||
$image_style = ImageStyle::load($this->configuration['images']['thumbnail_image_style']);
|
||||
if (!is_null($image_style)) {
|
||||
$summery[] = $this->t('Thumbnail style: @thumbnail_style', [
|
||||
'@thumbnail_style' => $image_style->label(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $summery;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return parent::defaultConfiguration() + [
|
||||
'images' => [
|
||||
'show_dimensions' => FALSE,
|
||||
'show_thumbnail' => FALSE,
|
||||
'thumbnail_image_style' => 'linkit_result_thumbnail',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
$dependencies = parent::calculateDependencies() + [
|
||||
'module' => ['file'],
|
||||
];
|
||||
|
||||
if ($this->configuration['images']['show_thumbnail']) {
|
||||
$dependencies['module'][] = 'image';
|
||||
$dependencies['config'][] = 'image.style.' . $this->configuration['images']['thumbnail_image_style'];
|
||||
}
|
||||
|
||||
return $dependencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::buildConfigurationForm($form, $form_state);
|
||||
|
||||
$form['images'] = array(
|
||||
'#type' => 'details',
|
||||
'#title' => t('Image file settings'),
|
||||
'#description' => t('Extra settings for image files in the result.'),
|
||||
'#tree' => TRUE,
|
||||
);
|
||||
|
||||
$form['images']['show_dimensions'] = [
|
||||
'#title' => t('Show pixel dimensions'),
|
||||
'#type' => 'checkbox',
|
||||
'#default_value' => $this->configuration['images']['show_dimensions'],
|
||||
];
|
||||
|
||||
if ($this->moduleHandler->moduleExists('image')) {
|
||||
$form['images']['show_thumbnail'] = [
|
||||
'#title' => t('Show thumbnail'),
|
||||
'#type' => 'checkbox',
|
||||
'#default_value' => $this->configuration['images']['show_thumbnail'],
|
||||
];
|
||||
|
||||
$form['images']['thumbnail_image_style'] = [
|
||||
'#title' => t('Thumbnail image style'),
|
||||
'#type' => 'select',
|
||||
'#default_value' => $this->configuration['images']['thumbnail_image_style'],
|
||||
'#options' => image_style_options(FALSE),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
':input[name="images[show_thumbnail]"]' => ['checked' => TRUE],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::submitConfigurationForm($form, $form_state);
|
||||
|
||||
$values = $form_state->getValue('images');
|
||||
if (!$values['show_thumbnail']) {
|
||||
$values['thumbnail_image_style'] = NULL;
|
||||
}
|
||||
|
||||
$this->configuration['images'] = $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function buildEntityQuery($match) {
|
||||
$query = parent::buildEntityQuery($match);
|
||||
$query->condition('status', FILE_STATUS_PERMANENT);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function buildDescription($entity) {
|
||||
$description_array = array();
|
||||
|
||||
$description_array[] = parent::buildDescription($entity);
|
||||
|
||||
/** @var \Drupal\file\FileInterface $entity */
|
||||
$file = $entity->getFileUri();
|
||||
|
||||
/** @var \Drupal\Core\Image\ImageInterface $image */
|
||||
$image = \Drupal::service('image.factory')->get($file);
|
||||
if ($image->isValid()) {
|
||||
if ($this->configuration['images']['show_dimensions']) {
|
||||
$description_array[] = $image->getWidth() . 'x' . $image->getHeight() . 'px';
|
||||
}
|
||||
|
||||
if ($this->configuration['images']['show_thumbnail'] && $this->moduleHandler->moduleExists('image')) {
|
||||
$image_element = array(
|
||||
'#weight' => -10,
|
||||
'#theme' => 'image_style',
|
||||
'#style_name' => $this->configuration['images']['thumbnail_image_style'],
|
||||
'#uri' => $entity->getFileUri(),
|
||||
);
|
||||
|
||||
$description_array[] = (string) \Drupal::service('renderer')->render($image_element);
|
||||
}
|
||||
}
|
||||
|
||||
$description = implode('<br />' , $description_array);
|
||||
return LinkitXss::descriptionFilter($description);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* The file entity still uses url() even though it's deprecated in the
|
||||
* entity interface.
|
||||
*/
|
||||
protected function buildPath($entity) {
|
||||
/** @var \Drupal\file\FileInterface $entity */
|
||||
return file_url_transform_relative(file_create_url($entity->getFileUri()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Matcher\NodeMatcher.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Matcher;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* @Matcher(
|
||||
* id = "entity:node",
|
||||
* target_entity = "node",
|
||||
* label = @Translation("Content"),
|
||||
* provider = "node"
|
||||
* )
|
||||
*/
|
||||
class NodeMatcher extends EntityMatcher {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSummary() {
|
||||
$summery = parent::getSummary();
|
||||
|
||||
$summery[] = $this->t('Include unpublished: @include_unpublished', [
|
||||
'@include_unpublished' => $this->configuration['include_unpublished'] ? $this->t('Yes') : $this->t('No'),
|
||||
]);
|
||||
|
||||
return $summery;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return parent::defaultConfiguration() + [
|
||||
'include_unpublished' => FALSE,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
return parent::calculateDependencies() + [
|
||||
'module' => ['node'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::buildConfigurationForm($form, $form_state);
|
||||
|
||||
$form['include_unpublished'] = [
|
||||
'#title' => t('Include unpublished nodes'),
|
||||
'#type' => 'checkbox',
|
||||
'#default_value' => $this->configuration['include_unpublished'],
|
||||
'#description' => t('In order to see unpublished nodes, the requesting user must also have permissions to do so.'),
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::submitConfigurationForm($form, $form_state);
|
||||
|
||||
$this->configuration['include_unpublished'] = $form_state->getValue('include_unpublished');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function buildEntityQuery($match) {
|
||||
$query = parent::buildEntityQuery($match);
|
||||
|
||||
$no_access = !$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'));
|
||||
if ($this->configuration['include_unpublished'] !== TRUE || $no_access) {
|
||||
$query->condition('status', NODE_PUBLISHED);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Matcher\TermMatcher.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Matcher;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\linkit\Utility\LinkitXss;
|
||||
|
||||
/**
|
||||
* @Matcher(
|
||||
* id = "entity:taxonomy_term",
|
||||
* target_entity = "taxonomy_term",
|
||||
* label = @Translation("Taxonomy term"),
|
||||
* provider = "taxonomy"
|
||||
* )
|
||||
*/
|
||||
class TermMatcher extends EntityMatcher {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
return parent::calculateDependencies() + [
|
||||
'module' => ['taxonomy'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::buildConfigurationForm($form, $form_state);
|
||||
$this->insertTokenList($form, ['term']);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function buildDescription($entity) {
|
||||
$description = \Drupal::token()->replace($this->configuration['result_description'], ['term' => $entity], []);
|
||||
return LinkitXss::descriptionFilter($description);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Plugin\Linkit\Matcher\UserMatcher.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Plugin\Linkit\Matcher;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* @Matcher(
|
||||
* id = "entity:user",
|
||||
* target_entity = "user",
|
||||
* label = @Translation("User"),
|
||||
* provider = "user"
|
||||
* )
|
||||
*/
|
||||
class UserMatcher extends EntityMatcher {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSummary() {
|
||||
$summery = parent::getSummary();
|
||||
|
||||
$roles = !empty($this->configuration['roles']) ? $this->configuration['roles'] : ['None'];
|
||||
$summery[] = $this->t('Role filter: @role_filter', [
|
||||
'@role_filter' => implode(', ', $roles),
|
||||
]);
|
||||
|
||||
$summery[] = $this->t('Include blocked users: @include_blocked', [
|
||||
'@include_blocked' => $this->configuration['include_blocked'] ? $this->t('Yes') : $this->t('No'),
|
||||
]);
|
||||
|
||||
return $summery;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return parent::defaultConfiguration() + [
|
||||
'roles' => [],
|
||||
'include_blocked' => FALSE,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
return parent::calculateDependencies() + [
|
||||
'module' => ['user'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::buildConfigurationForm($form, $form_state);
|
||||
|
||||
$form['roles'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => $this->t('Restrict to the selected roles'),
|
||||
'#options' => array_diff_key(user_role_names(TRUE), array(RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID)),
|
||||
'#default_value' => $this->configuration['roles'],
|
||||
'#description' => $this->t('If none of the checkboxes is checked, allow all roles.'),
|
||||
'#element_validate' => [[get_class($this), 'elementValidateFilter']],
|
||||
);
|
||||
|
||||
$form['include_blocked'] = [
|
||||
'#title' => t('Include blocked user'),
|
||||
'#type' => 'checkbox',
|
||||
'#default_value' => $this->configuration['include_blocked'],
|
||||
'#description' => t('In order to see blocked users, the requesting user must also have permissions to do so.'),
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::submitConfigurationForm($form, $form_state);
|
||||
|
||||
$this->configuration['roles'] = $form_state->getValue('roles');
|
||||
$this->configuration['include_blocked'] = $form_state->getValue('include_blocked');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function buildEntityQuery($match) {
|
||||
$query = parent::buildEntityQuery($match);
|
||||
|
||||
$match = $this->database->escapeLike($match);
|
||||
// The user entity don't specify a label key so we have to do it instead.
|
||||
$query->condition('name', '%' . $match . '%', 'LIKE');
|
||||
|
||||
// Filter by role.
|
||||
if (!empty($this->configuration['roles'])) {
|
||||
$query->condition('roles', $this->configuration['roles'], 'IN');
|
||||
}
|
||||
|
||||
if ($this->configuration['include_blocked'] !== TRUE || !$this->currentUser->hasPermission('administer users')) {
|
||||
$query->condition('status', 1);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\ProfileInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining a profile entity.
|
||||
*/
|
||||
interface ProfileInterface extends ConfigEntityInterface {
|
||||
|
||||
/**
|
||||
* Gets the profile description.
|
||||
*
|
||||
* @return string
|
||||
* The profile description.
|
||||
*/
|
||||
public function getDescription();
|
||||
|
||||
/**
|
||||
* Sets the profile description.
|
||||
*
|
||||
* @param string $description
|
||||
* The profile description.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDescription($description);
|
||||
|
||||
/**
|
||||
* Returns a specific attribute.
|
||||
*
|
||||
* @param string $attribute_id
|
||||
* The attribute ID.
|
||||
*
|
||||
* @return \Drupal\linkit\AttributeInterface
|
||||
* The attribute object.
|
||||
*/
|
||||
public function getAttribute($attribute_id);
|
||||
|
||||
/**
|
||||
* Returns the attributes for this profile.
|
||||
*
|
||||
* @return \Drupal\linkit\AttributeCollection|\Drupal\linkit\AttributeInterface[]
|
||||
* The attribute collection.
|
||||
*/
|
||||
public function getAttributes();
|
||||
|
||||
/**
|
||||
* Adds an attribute to this profile.
|
||||
*
|
||||
* @param array $configuration
|
||||
* An array of attribute configuration.
|
||||
*
|
||||
* @return String
|
||||
* The ID of the attribute.
|
||||
*/
|
||||
public function addAttribute(array $configuration);
|
||||
|
||||
/**
|
||||
* Removes an attribute from this profile.
|
||||
*
|
||||
* @param string $attribute_id
|
||||
* The attribute ID.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function removeAttribute($attribute_id);
|
||||
|
||||
/**
|
||||
* Sets the configuration for an attribute instance.
|
||||
*
|
||||
* @param string $attribute_id
|
||||
* The ID of the attribute to set the configuration for.
|
||||
* @param array $configuration
|
||||
* The attribute configuration to set.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setAttributeConfig($attribute_id, array $configuration);
|
||||
|
||||
/**
|
||||
* Returns a specific matcher.
|
||||
*
|
||||
* @param string $instance_id
|
||||
* The matcher instance ID.
|
||||
*
|
||||
* @return \Drupal\linkit\MatcherInterface
|
||||
* The matcher object.
|
||||
*/
|
||||
public function getMatcher($instance_id);
|
||||
|
||||
/**
|
||||
* Returns the matchers for this profile.
|
||||
*
|
||||
* @return \Drupal\linkit\MatcherCollection|\Drupal\linkit\MatcherInterface[]
|
||||
* The matcher collection.
|
||||
*/
|
||||
public function getMatchers();
|
||||
|
||||
/**
|
||||
* Adds a matcher to this profile.
|
||||
*
|
||||
* @param array $configuration
|
||||
* An array of matcher configuration.
|
||||
*
|
||||
* @return string
|
||||
* The instance ID of the matcher.
|
||||
*/
|
||||
public function addMatcher(array $configuration);
|
||||
|
||||
/**
|
||||
* Removes a matcher from this profile.
|
||||
*
|
||||
* @param \Drupal\linkit\MatcherInterface $matcher
|
||||
* The matcher object.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function removeMatcher(MatcherInterface $matcher);
|
||||
|
||||
/**
|
||||
* Sets the configuration for a matcher instance.
|
||||
*
|
||||
* @param string $instance_id
|
||||
* The instance ID of the matcher to set the configuration for.
|
||||
* @param array $configuration
|
||||
* The matcher configuration to set.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setMatcherConfig($instance_id, array $configuration);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\ProfileListBuilder.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Defines a class to build a listing of profile entities.
|
||||
*
|
||||
* @see \Drupal\linkit\Entity\Profile
|
||||
*/
|
||||
class ProfileListBuilder extends ConfigEntityListBuilder {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildHeader() {
|
||||
$header['title'] = t('Profile');
|
||||
$header['description'] = [
|
||||
'data' => t('Description'),
|
||||
'class' => [RESPONSIVE_PRIORITY_MEDIUM],
|
||||
];
|
||||
return $header + parent::buildHeader();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildRow(EntityInterface $entity) {
|
||||
/** @var \Drupal\linkit\ProfileInterface $linkitProfile */
|
||||
$linkitProfile = $entity;
|
||||
$row['label'] = $linkitProfile->label();
|
||||
$row['description']['data'] = ['#markup' => $linkitProfile->getDescription()];
|
||||
return $row + parent::buildRow($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultOperations(EntityInterface $entity) {
|
||||
$operations = parent::getDefaultOperations($entity);
|
||||
|
||||
if (isset($operations['edit'])) {
|
||||
$operations['edit']['title'] = t('Edit profile');
|
||||
}
|
||||
|
||||
$operations['matchers'] = [
|
||||
'title' => t('Manage matchers'),
|
||||
'weight' => 10,
|
||||
'url' => Url::fromRoute('linkit.matchers', [
|
||||
'linkit_profile' => $entity->id()
|
||||
]),
|
||||
];
|
||||
|
||||
$operations['attributes'] = [
|
||||
'title' => t('Manage attributes'),
|
||||
'weight' => 20,
|
||||
'url' => Url::fromRoute('linkit.attributes', [
|
||||
'linkit_profile' => $entity->id()
|
||||
]),
|
||||
];
|
||||
|
||||
return $operations;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\ResultManager.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit;
|
||||
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Result service to handle autocomplete matcher results.
|
||||
*/
|
||||
class ResultManager {
|
||||
|
||||
/**
|
||||
* Gets the results.
|
||||
*
|
||||
* @param ProfileInterface $linkitProfile
|
||||
* The linkit profile.
|
||||
* @param $search_string
|
||||
* The string ro use in the matchers.
|
||||
*
|
||||
* @return array
|
||||
* An array of matches.
|
||||
*/
|
||||
public function getResults(ProfileInterface $linkitProfile, $search_string) {
|
||||
$matches = array();
|
||||
|
||||
if (empty(trim($search_string))) {
|
||||
return [[
|
||||
'title' => t('No results'),
|
||||
]];
|
||||
}
|
||||
|
||||
// Special for link to front page.
|
||||
if (strpos($search_string, 'front') !== FALSE) {
|
||||
$matches[] = [
|
||||
'title' => t('Front page'),
|
||||
'description' => 'The front page for this site.',
|
||||
'path' => Url::fromRoute('<front>')->toString(),
|
||||
'group' => t('System'),
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($linkitProfile->getMatchers() as $plugin) {
|
||||
$matches = array_merge($matches, $plugin->getMatches($search_string));
|
||||
}
|
||||
|
||||
// Check for an e-mail address then return an e-mail match and create a
|
||||
// mail-to link if appropriate.
|
||||
if (filter_var($search_string, FILTER_VALIDATE_EMAIL)) {
|
||||
$matches[] = [
|
||||
'title' => t('E-mail @email', ['@email' => $search_string]),
|
||||
'description' => t('Opens your mail client ready to e-mail @email', ['@email' => $search_string]),
|
||||
'path' => 'mailto:' . Html::escape($search_string),
|
||||
'group' => t('E-mail'),
|
||||
];
|
||||
}
|
||||
|
||||
// If there is still no matches, return a "no results" array.
|
||||
if (empty($matches)) {
|
||||
return [[
|
||||
'title' => t('No results'),
|
||||
]];
|
||||
}
|
||||
|
||||
return $matches;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Tests\AttributeCrudTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Tests;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\Entity\Profile;
|
||||
|
||||
/**
|
||||
* Tests adding, listing and deleting attributes on a profile.
|
||||
*
|
||||
* @group linkit
|
||||
*/
|
||||
class AttributeCrudTest extends LinkitTestBase {
|
||||
|
||||
/**
|
||||
* The attribute manager.
|
||||
*
|
||||
* @var \Drupal\linkit\AttributeManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* The linkit profile.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->manager = $this->container->get('plugin.manager.linkit.attribute');
|
||||
|
||||
$this->linkitProfile = $this->createProfile();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the overview page.
|
||||
*/
|
||||
function testOverview() {
|
||||
$this->drupalGet(Url::fromRoute('linkit.attributes', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
$this->assertText(t('No attributes added.'));
|
||||
|
||||
$this->assertLinkByHref(Url::fromRoute('linkit.attribute.add', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
])->toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test adding an attribute to a profile.
|
||||
*/
|
||||
function testAdd() {
|
||||
$this->drupalGet(Url::fromRoute('linkit.attribute.add', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
|
||||
$this->assertEqual(count($this->manager->getDefinitions()), count($this->xpath('//input[@type="radio"]')), 'All attributes are available.');
|
||||
|
||||
$edit = array();
|
||||
$edit['plugin'] = 'dummy_attribute';
|
||||
$this->drupalPostForm(NULL, $edit, t('Save and continue'));
|
||||
|
||||
$this->assertUrl(Url::fromRoute('linkit.attributes', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
|
||||
$this->assertEqual(1, count($this->xpath('//table/tbody/tr')), 'Attribute added.');
|
||||
$this->assertNoText(t('No attributes added.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test adding a configurable attribute to a profile.
|
||||
*/
|
||||
function testAddConfigurable() {
|
||||
$this->drupalGet(Url::fromRoute('linkit.attribute.add', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
|
||||
$this->assertEqual(count($this->manager->getDefinitions()), count($this->xpath('//input[@type="radio"]')), 'All attributes are available.');
|
||||
|
||||
$edit = array();
|
||||
$edit['plugin'] = 'configurable_dummy_attribute';
|
||||
$this->drupalPostForm(NULL, $edit, t('Save and continue'));
|
||||
|
||||
$this->assertUrl(Url::fromRoute('linkit.attribute.edit', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => 'configurable_dummy_attribute',
|
||||
]));
|
||||
|
||||
$this->drupalGet(Url::fromRoute('linkit.attributes', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
|
||||
$this->assertEqual(1, count($this->xpath('//table/tbody/tr')), 'Attribute added.');
|
||||
$this->assertNoText(t('No attributes added.'));
|
||||
|
||||
$plugin_url = Url::fromRoute('linkit.attribute.edit', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => 'configurable_dummy_attribute',
|
||||
]);
|
||||
|
||||
$this->assertLinkByHref($plugin_url->toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete an attribute from a profile.
|
||||
*/
|
||||
function testDelete() {
|
||||
/** @var \Drupal\linkit\AttributeInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('dummy_attribute');
|
||||
|
||||
$this->linkitProfile->addAttribute($plugin->getConfiguration());
|
||||
$this->linkitProfile->save();
|
||||
|
||||
// Try delete an attribute that is not attached to the profile.
|
||||
$this->drupalGet(Url::fromRoute('linkit.attribute.delete', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => 'doesntexists'
|
||||
]));
|
||||
$this->assertResponse('404');
|
||||
|
||||
// Go to the delete page, but press cancel.
|
||||
$this->drupalGet(Url::fromRoute('linkit.attribute.delete', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $plugin->getPluginId(),
|
||||
]));
|
||||
$this->clickLink(t('Cancel'));
|
||||
$this->assertUrl(Url::fromRoute('linkit.attributes', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
|
||||
// Delete the attribute from the profile.
|
||||
$this->drupalGet(Url::fromRoute('linkit.attribute.delete', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => 'dummy_attribute',
|
||||
]));
|
||||
|
||||
$this->drupalPostForm(NULL, [], t('Confirm'));
|
||||
$this->assertRaw(t('The attribute %plugin has been deleted.', ['%plugin' => $plugin->getLabel()]));
|
||||
$this->assertUrl(Url::fromRoute('linkit.attributes', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
$this->assertText(t('No attributes added.'));
|
||||
|
||||
/** @var \Drupal\linkit\Entity\Profile $updated_profile */
|
||||
$updated_profile = Profile::load($this->linkitProfile->id());
|
||||
$this->assertFalse($updated_profile->getAttributes()->has($plugin->getPluginId()), 'The attribute is deleted from the profile');
|
||||
}
|
||||
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Tests\Controllers\LinkitControllerTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Tests\Controllers;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\Tests\LinkitTestBase;
|
||||
|
||||
|
||||
/**
|
||||
* Tests Linkit controller.
|
||||
*
|
||||
* @group linkit
|
||||
*/
|
||||
class LinkitControllerTest extends LinkitTestBase {
|
||||
|
||||
/**
|
||||
* The linkit profile.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->linkitProfile = $this->createProfile();
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the profile route title callback.
|
||||
*/
|
||||
function testProfileTitle() {
|
||||
$this->drupalGet(Url::fromRoute('entity.linkit_profile.edit_form', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
|
||||
$this->assertText('Edit ' . $this->linkitProfile->label() . ' profile');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the matcher route title callback.
|
||||
*/
|
||||
function testMatcherTitle() {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->container->get('plugin.manager.linkit.matcher')->createInstance('configurable_dummy_matcher');
|
||||
$matcher_uuid = $this->linkitProfile->addMatcher($plugin->getConfiguration());
|
||||
$this->linkitProfile->save();
|
||||
|
||||
$this->drupalGet(Url::fromRoute('linkit.matcher.edit', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $matcher_uuid,
|
||||
]));
|
||||
|
||||
$this->assertText('Edit ' . $plugin->getLabel() . ' matcher');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the attribute route title callback.
|
||||
*/
|
||||
function testAttributeTitle() {
|
||||
/** @var \Drupal\linkit\AttributeInterface $plugin */
|
||||
$plugin = $this->container->get('plugin.manager.linkit.attribute')->createInstance('configurable_dummy_attribute');
|
||||
$this->linkitProfile->addAttribute($plugin->getConfiguration());
|
||||
$this->linkitProfile->save();
|
||||
|
||||
$this->drupalGet(Url::fromRoute('linkit.attribute.edit', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $plugin->getPluginId(),
|
||||
]));
|
||||
$this->assertText('Edit ' . $plugin->getLabel() . ' attribute');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Tests\LinkitTestBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Tests;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\linkit\Entity\Profile;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Sets up page and article content types.
|
||||
*/
|
||||
abstract class LinkitTestBase extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* Enable block module to get the local_actions_block to work.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['linkit', 'linkit_test', 'block'];
|
||||
|
||||
/**
|
||||
* A user with the 'administer linkit profiles' permission.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* A user without the 'administer linkit profiles' permission.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $baseUser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->adminUser = $this->drupalCreateUser(['administer linkit profiles']);
|
||||
$this->baseUser = $this->drupalCreateUser();
|
||||
|
||||
$this->drupalPlaceBlock('page_title_block', ['region' => 'content']);
|
||||
$this->drupalPlaceBlock('local_tasks_block', ['region' => 'content']);
|
||||
$this->drupalPlaceBlock('local_actions_block', ['region' => 'content']);
|
||||
$this->drupalPlaceBlock('system_messages_block', ['region' => 'highlighted']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a profile based on default settings.
|
||||
*
|
||||
* @param array $settings
|
||||
* (optional) An associative array of settings for the profile, as used in
|
||||
* entity_create(). Override the defaults by specifying the key and value
|
||||
* in the array
|
||||
*
|
||||
* The following defaults are provided:
|
||||
* - label: Random string.
|
||||
*
|
||||
* @return \Drupal\linkit\ProfileInterface
|
||||
* The created profile entity.
|
||||
*/
|
||||
protected function createProfile(array $settings = []) {
|
||||
// Populate defaults array.
|
||||
$settings += [
|
||||
'id' => Unicode::strtolower($this->randomMachineName()),
|
||||
'label' => $this->randomMachineName(),
|
||||
];
|
||||
|
||||
$profile = Profile::create($settings);
|
||||
$profile->save();
|
||||
|
||||
return $profile;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Tests\MatcherCrudTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Tests;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\linkit\Entity\Profile;
|
||||
|
||||
/**
|
||||
* Tests adding, listing, updating and deleting matchers on a profile.
|
||||
*
|
||||
* @group linkit
|
||||
*/
|
||||
class MatcherCrudTest extends LinkitTestBase {
|
||||
|
||||
/**
|
||||
* The attribute manager.
|
||||
*
|
||||
* @var \Drupal\linkit\MatcherManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* The linkit profile.
|
||||
*
|
||||
* @var \Drupal\linkit\ProfileInterface
|
||||
*/
|
||||
protected $linkitProfile;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
|
||||
|
||||
$this->linkitProfile = $this->createProfile();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the overview page.
|
||||
*/
|
||||
function testOverview() {
|
||||
$this->drupalGet(Url::fromRoute('linkit.matchers', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
$this->assertText(t('No matchers added.'));
|
||||
|
||||
$this->assertLinkByHref(Url::fromRoute('linkit.matcher.add', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
])->toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test adding a matcher to a profile.
|
||||
*/
|
||||
function testAdd() {
|
||||
$this->drupalGet(Url::fromRoute('linkit.matcher.add', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
|
||||
$edit = array();
|
||||
$edit['plugin'] = 'dummy_matcher';
|
||||
$this->drupalPostForm(NULL, $edit, t('Save and continue'));
|
||||
|
||||
// Load the saved profile.
|
||||
$this->linkitProfile = Profile::load($this->linkitProfile->id());
|
||||
|
||||
$matcher_ids = $this->linkitProfile->getMatchers()->getInstanceIds();
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->linkitProfile->getMatcher(current($matcher_ids));
|
||||
|
||||
$this->assertRaw(t('Added %label matcher.', ['%label' => $plugin->getLabel()]));
|
||||
$this->assertNoText(t('No matchers added.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test adding a configurable attribute to a profile.
|
||||
*/
|
||||
function testAddConfigurable() {
|
||||
$this->drupalGet(Url::fromRoute('linkit.matcher.add', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
|
||||
$edit = array();
|
||||
$edit['plugin'] = 'configurable_dummy_matcher';
|
||||
$this->drupalPostForm(NULL, $edit, t('Save and continue'));
|
||||
|
||||
// Load the saved profile.
|
||||
$this->linkitProfile = Profile::load($this->linkitProfile->id());
|
||||
|
||||
$matcher_ids = $this->linkitProfile->getMatchers()->getInstanceIds();
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->linkitProfile->getMatcher(current($matcher_ids));
|
||||
|
||||
$this->assertUrl(Url::fromRoute('linkit.matcher.edit', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
'plugin_instance_id' => $plugin->getUuid(),
|
||||
]));
|
||||
|
||||
$this->drupalGet(Url::fromRoute('linkit.matchers', [
|
||||
'linkit_profile' => $this->linkitProfile->id(),
|
||||
]));
|
||||
|
||||
$this->assertNoText(t('No matchers added.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete a matcher from a profile.
|
||||
*/
|
||||
function testDelete() {
|
||||
/** @var \Drupal\linkit\AttributeInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('dummy_matcher');
|
||||
|
||||
$profile = $this->createProfile();
|
||||
$plugin_uuid = $profile->addMatcher($plugin->getConfiguration());
|
||||
$profile->save();
|
||||
|
||||
// Try delete a matcher that is not attached to the profile.
|
||||
$this->drupalGet(Url::fromRoute('linkit.matcher.delete', [
|
||||
'linkit_profile' => $profile->id(),
|
||||
'plugin_instance_id' => 'doesntexists'
|
||||
]));
|
||||
$this->assertResponse('404');
|
||||
|
||||
// Go to the delete page, but press cancel.
|
||||
$this->drupalGet(Url::fromRoute('linkit.matcher.delete', [
|
||||
'linkit_profile' => $profile->id(),
|
||||
'plugin_instance_id' => $plugin_uuid,
|
||||
]));
|
||||
$this->clickLink(t('Cancel'));
|
||||
$this->assertUrl(Url::fromRoute('linkit.matchers', [
|
||||
'linkit_profile' => $profile->id(),
|
||||
]));
|
||||
|
||||
// Delete the matcher from the profile.
|
||||
$this->drupalGet(Url::fromRoute('linkit.matcher.delete', [
|
||||
'linkit_profile' => $profile->id(),
|
||||
'plugin_instance_id' => $plugin_uuid,
|
||||
]));
|
||||
|
||||
$this->drupalPostForm(NULL, [], t('Confirm'));
|
||||
$this->assertRaw(t('The matcher %plugin has been deleted.', ['%plugin' => $plugin->getLabel()]));
|
||||
$this->assertUrl(Url::fromRoute('linkit.matchers', [
|
||||
'linkit_profile' => $profile->id(),
|
||||
]));
|
||||
$this->assertText(t('No matchers added.'));
|
||||
|
||||
/** @var \Drupal\linkit\Entity\Profile $updated_profile */
|
||||
$updated_profile = Profile::load($profile->id());
|
||||
$this->assertFalse($updated_profile->getMatchers()->has($plugin_uuid), 'The user matcher is deleted from the profile');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Tests\Matchers\NodeMatcherTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Tests\Matchers;
|
||||
|
||||
use Drupal\linkit\Tests\LinkitTestBase;
|
||||
|
||||
/**
|
||||
* Tests node matcher.
|
||||
*
|
||||
* @group linkit
|
||||
*/
|
||||
class NodeMatcherTest extends LinkitTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['node'];
|
||||
|
||||
/**
|
||||
* The matcher manager.
|
||||
*
|
||||
* @var \Drupal\linkit\MatcherManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
|
||||
|
||||
$type1 = $this->drupalCreateContentType(['type' => 'test1', 'name' => 'Test1']);
|
||||
$type2 = $this->drupalCreateContentType(['type' => 'test2', 'name' => 'Test2']);
|
||||
|
||||
// Nodes with type 1.
|
||||
$this->drupalCreateNode(['title' => 'Lorem Ipsum 1', 'type' => $type1->id()]);
|
||||
$this->drupalCreateNode(['title' => 'Lorem Ipsum 2', 'type' => $type1->id()]);
|
||||
|
||||
// Nodes with type 1.
|
||||
$this->drupalCreateNode(['title' => 'Lorem Ipsum 3', 'type' => $type2->id()]);
|
||||
|
||||
// Unpublished node.
|
||||
$this->drupalCreateNode(['title' => 'Lorem unpublishd', 'type' => $type1->id(), 'status' => FALSE]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests node matcher.
|
||||
*/
|
||||
function testNodeMatcherWidthDefaultConfiguration() {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('entity:node', []);
|
||||
$matches = $plugin->getMatches('Lorem');
|
||||
$this->assertEqual(3, count($matches), 'Correct number of matches');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests node matcher with bundle filer.
|
||||
*/
|
||||
function testNodeMatcherWidthBundleFiler() {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('entity:node', [
|
||||
'settings' => [
|
||||
'bundles' => [
|
||||
'test1' => 'test1'
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$matches = $plugin->getMatches('Lorem');
|
||||
$this->assertEqual(2, count($matches), 'Correct number of matches');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests node matcher with include unpublished setting activated.
|
||||
*/
|
||||
function testNodeMatcherWidthIncludeUnpublished() {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('entity:node', [
|
||||
'settings' => [
|
||||
'include_unpublished' => TRUE,
|
||||
],
|
||||
]);
|
||||
|
||||
// Test without permissions to see unpublished nodes.
|
||||
$matches = $plugin->getMatches('Lorem');
|
||||
$this->assertEqual(3, count($matches), 'Correct number of matches');
|
||||
|
||||
$account = $this->drupalCreateUser(['bypass node access']);
|
||||
$this->drupalLogin($account);
|
||||
|
||||
// Test with permissions to see unpublished nodes.
|
||||
$matches = $plugin->getMatches('Lorem');
|
||||
$this->assertEqual(4, count($matches), 'Correct number of matches');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Tests\Matchers\TermMatcherTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Tests\Matchers;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\linkit\Tests\LinkitTestBase;
|
||||
use Drupal\taxonomy\Entity\Vocabulary;
|
||||
|
||||
/**
|
||||
* Tests term matcher.
|
||||
*
|
||||
* @group linkit
|
||||
*/
|
||||
class TermMatcherTest extends LinkitTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['taxonomy'];
|
||||
|
||||
/**
|
||||
* The matcher manager.
|
||||
*
|
||||
* @var \Drupal\linkit\MatcherManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* Creates and saves a vocabulary.
|
||||
*
|
||||
* @param string $name
|
||||
* The vocabulary name.
|
||||
*
|
||||
* @return Vocabulary The new vocabulary object.
|
||||
* The new vocabulary object.
|
||||
*/
|
||||
private function createVocabulary($name) {
|
||||
$vocabularyStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary');
|
||||
$vocabulary = $vocabularyStorage->create([
|
||||
'name' => $name,
|
||||
'description' => $name,
|
||||
'vid' => Unicode::strtolower($name),
|
||||
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
||||
]);
|
||||
$vocabulary->save();
|
||||
return $vocabulary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and saves a new term with in vocabulary $vid.
|
||||
*
|
||||
* @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
|
||||
* The vocabulary object.
|
||||
* @param array $values
|
||||
* (optional) An array of values to set, keyed by property name. If the
|
||||
* entity type has bundles, the bundle key has to be specified.
|
||||
*
|
||||
* @return \Drupal\taxonomy\Entity\Term
|
||||
* The new taxonomy term object.
|
||||
*/
|
||||
private function createTerm(Vocabulary $vocabulary, $values = array()) {
|
||||
$filter_formats = filter_formats();
|
||||
$format = array_pop($filter_formats);
|
||||
|
||||
$termStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
|
||||
$term = $termStorage->create($values + array(
|
||||
'name' => $this->randomMachineName(),
|
||||
'description' => array(
|
||||
'value' => $this->randomMachineName(),
|
||||
// Use the first available text format.
|
||||
'format' => $format->id(),
|
||||
),
|
||||
'vid' => $vocabulary->id(),
|
||||
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
||||
));
|
||||
$term->save();
|
||||
return $term;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
|
||||
|
||||
$testing_vocabulary_1 = $this->createVocabulary('testing_vocabulary_1');
|
||||
$testing_vocabulary_2 = $this->createVocabulary('testing_vocabulary_2');
|
||||
|
||||
$this->createTerm($testing_vocabulary_1, ['name' => 'foo_bar']);
|
||||
$this->createTerm($testing_vocabulary_1, ['name' => 'foo_baz']);
|
||||
$this->createTerm($testing_vocabulary_1, ['name' => 'foo_foo']);
|
||||
$this->createTerm($testing_vocabulary_1, ['name' => 'bar']);
|
||||
$this->createTerm($testing_vocabulary_2, ['name' => 'foo_bar']);
|
||||
$this->createTerm($testing_vocabulary_2, ['name' => 'foo_baz']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests term matcher with default configuration.
|
||||
*/
|
||||
function testTermMatcherWidthDefaultConfiguration() {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('entity:taxonomy_term', []);
|
||||
$matches = $plugin->getMatches('foo');
|
||||
$this->assertEqual(5, count($matches), 'Correct number of matches');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests term matcher with bundle filer.
|
||||
*/
|
||||
function testTermMatcherWidthBundleFiler() {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('entity:taxonomy_term', [
|
||||
'settings' => [
|
||||
'bundles' => [
|
||||
'testing_vocabulary_1' => 'testing_vocabulary_1'
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$matches = $plugin->getMatches('foo');
|
||||
$this->assertEqual(3, count($matches), 'Correct number of matches');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Tests\Matchers\UserMatcherTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Tests\Matchers;
|
||||
|
||||
use Drupal\linkit\Tests\LinkitTestBase;
|
||||
|
||||
/**
|
||||
* Tests user matcher.
|
||||
*
|
||||
* @group linkit
|
||||
*/
|
||||
class UserMatcherTest extends LinkitTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['user'];
|
||||
|
||||
/**
|
||||
* The matcher manager.
|
||||
*
|
||||
* @var \Drupal\linkit\MatcherManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalLogin($this->drupalCreateUser(['access user profiles']));
|
||||
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
|
||||
|
||||
$custom_role = $this->drupalCreateRole(array(), 'custom_role', 'custom_role');
|
||||
$custom_role_admin = $this->drupalCreateRole(array(), 'custom_role_admin', 'custom_role_admin');
|
||||
|
||||
$this->drupalCreateUser([], 'lorem');
|
||||
$this->drupalCreateUser([], 'foo');
|
||||
|
||||
$account = $this->drupalCreateUser([], 'ipsumlorem');
|
||||
$account->addRole($custom_role);
|
||||
$account->save();
|
||||
|
||||
$account = $this->drupalCreateUser([], 'lorem_custom_role');
|
||||
$account->addRole($custom_role);
|
||||
$account->save();
|
||||
|
||||
$account = $this->drupalCreateUser([], 'lorem_custom_role_admin');
|
||||
$account->addRole($custom_role_admin);
|
||||
$account->save();
|
||||
|
||||
$account = $this->drupalCreateUser([], 'blocked_lorem');
|
||||
$account->block();
|
||||
$account->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests user matcher.
|
||||
*/
|
||||
function testUserMatcherWidthDefaultConfiguration() {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('entity:user', []);
|
||||
$matches = $plugin->getMatches('Lorem');
|
||||
$this->assertEqual(4, count($matches), 'Correct number of matches');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests user matcher with role filer.
|
||||
*/
|
||||
function testUserMatcherWidthRoleFiler() {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('entity:user', [
|
||||
'settings' => [
|
||||
'roles' => [
|
||||
'custom_role' => 'custom_role'
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$matches = $plugin->getMatches('Lorem');
|
||||
$this->assertEqual(2, count($matches), 'Correct number of matches');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests user matcher with include blocked setting activated.
|
||||
*/
|
||||
function testUserMatcherWidthIncludeBlocked() {
|
||||
/** @var \Drupal\linkit\MatcherInterface $plugin */
|
||||
$plugin = $this->manager->createInstance('entity:user', [
|
||||
'settings' => [
|
||||
'include_blocked' => TRUE,
|
||||
],
|
||||
]);
|
||||
|
||||
// Test without permissions to see blocked users.
|
||||
$matches = $plugin->getMatches('blocked');
|
||||
$this->assertEqual(0, count($matches), 'Correct number of matches');
|
||||
|
||||
$account = $this->drupalCreateUser(['administer users']);
|
||||
$this->drupalLogin($account);
|
||||
|
||||
// Test with permissions to see blocked users.
|
||||
$matches = $plugin->getMatches('blocked');
|
||||
$this->assertEqual(1, count($matches), 'Correct number of matches');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Tests\ProfileCreationTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Tests;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Tests creating, loading and deleting profiles.
|
||||
*
|
||||
* @group linkit
|
||||
*/
|
||||
class ProfileCrudTest extends LinkitTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the overview page.
|
||||
*/
|
||||
function testOverview() {
|
||||
// Verify that the profile collection page is not accessible for regular
|
||||
// users.
|
||||
$this->drupalLogin($this->baseUser);
|
||||
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
|
||||
$this->assertResponse(403);
|
||||
$this->drupalLogout();
|
||||
|
||||
// Verify that the profile collection page is accessible for regular users.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
$profiles = [];
|
||||
$profiles[] = $this->createProfile();
|
||||
$profiles[] = $this->createProfile();
|
||||
|
||||
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Assert that the 'Add profile' action exists.
|
||||
$this->assertLinkByHref(Url::fromRoute('entity.linkit_profile.add_form')->toString());
|
||||
|
||||
/** @var \Drupal\linkit\ProfileInterface $profile */
|
||||
foreach ($profiles as $profile) {
|
||||
$this->assertLinkByHref('admin/config/content/linkit/manage/' . $profile->id());
|
||||
$this->assertLinkByHref('admin/config/content/linkit/manage/' . $profile->id() . '/delete');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates profile.
|
||||
*/
|
||||
function testProfileCreation() {
|
||||
$this->drupalGet(Url::fromRoute('entity.linkit_profile.add_form'));
|
||||
$this->drupalGet('admin/config/content/linkit/add');
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Create a profile.
|
||||
$edit = [];
|
||||
$edit['label'] = Unicode::strtolower($this->randomMachineName());
|
||||
$edit['id'] = Unicode::strtolower($this->randomMachineName());
|
||||
$edit['description'] = $this->randomMachineName(16);
|
||||
$this->drupalPostForm(NULL, $edit, t('Save and manage matchers'));
|
||||
|
||||
$this->assertRaw(t('Created new profile %label.', ['%label' => $edit['label']]));
|
||||
|
||||
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
|
||||
$this->assertText($edit['label'], 'Profile exists in the profile collection.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a profile.
|
||||
*/
|
||||
function testProfileUpdate() {
|
||||
$profile = $this->createProfile();
|
||||
$this->drupalGet(Url::fromRoute('entity.linkit_profile.edit_form', [
|
||||
'linkit_profile' => $profile->id(),
|
||||
]));
|
||||
$this->assertResponse(200);
|
||||
|
||||
$id_field = $this->xpath('.//input[not(@disabled) and @name="id"]');
|
||||
|
||||
$this->assertTrue(empty($id_field), 'Machine name field is disabled.');
|
||||
$this->assertLinkByHref(Url::fromRoute('entity.linkit_profile.edit_form', [
|
||||
'linkit_profile' => $profile->id(),
|
||||
])->toString());
|
||||
$this->assertLinkByHref('admin/config/content/linkit/manage/' . $profile->id() . '/delete');
|
||||
|
||||
$edit = [];
|
||||
$edit['label'] = $this->randomMachineName();
|
||||
$edit['description'] = $this->randomMachineName(16);
|
||||
$this->drupalPostForm(NULL, $edit, t('Update profile'));
|
||||
|
||||
$this->assertRaw(t('Updated profile %label.', ['%label' => $edit['label']]));
|
||||
|
||||
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
|
||||
$this->assertText($edit['label'], 'Updated profile exists in the profile collection.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a profile.
|
||||
*/
|
||||
function testProfileDelete() {
|
||||
/** @var \Drupal\linkit\ProfileInterface $profile */
|
||||
$profile = $this->createProfile();
|
||||
$this->drupalGet(Url::fromRoute('entity.linkit_profile.delete_form', [
|
||||
'linkit_profile' => $profile->id(),
|
||||
]));
|
||||
$this->drupalPostForm(NULL, [], t('Delete'));
|
||||
|
||||
$this->assertRaw(t('The linkit profile %label has been deleted.', ['%label' => $profile->label()]));
|
||||
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
|
||||
$this->assertNoText($profile->label(), 'Deleted profile does not exists in the profile collection.');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\linkit\Utility\LinkitXss.
|
||||
*/
|
||||
|
||||
namespace Drupal\linkit\Utility;
|
||||
|
||||
use Drupal\Component\Utility\Xss;
|
||||
|
||||
/**
|
||||
* Extends the default XSS protection to simplify it for Linkits needs.
|
||||
*/
|
||||
class LinkitXss extends Xss {
|
||||
|
||||
/**
|
||||
* Description filter helper.
|
||||
*
|
||||
* @param $string
|
||||
* The string with raw HTML in it. It will be stripped of everything that
|
||||
* can cause an XSS attack.
|
||||
*
|
||||
* @return string
|
||||
* An XSS safe version of $string, or an empty string if $string is not
|
||||
* valid UTF-8.
|
||||
*
|
||||
* @see \Drupal\Component\Utility\Xss::filter()
|
||||
*/
|
||||
public static function descriptionFilter($string) {
|
||||
return parent::filter($string, ['img'] + Xss::getHtmlTagList());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user