abstract class LinkStatusHandlerBase in Link checker 8
Base class for Link status handler plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\linkchecker\Plugin\LinkStatusHandlerBase implements ContainerFactoryPluginInterface, LinkStatusHandlerInterface
Expanded class hierarchy of LinkStatusHandlerBase
2 files declare their use of LinkStatusHandlerBase
- Repair301.php in src/
Plugin/ LinkStatusHandler/ Repair301.php - Unpublish404.php in src/
Plugin/ LinkStatusHandler/ Unpublish404.php
File
- src/
Plugin/ LinkStatusHandlerBase.php, line 21
Namespace
Drupal\linkchecker\PluginView source
abstract class LinkStatusHandlerBase extends PluginBase implements LinkStatusHandlerInterface, ContainerFactoryPluginInterface {
/**
* The queue.
*
* @var \Drupal\Core\Queue\QueueInterface
*/
protected $queue;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* The account switcher.
*
* @var \Drupal\Core\Session\AccountSwitcherInterface
*/
protected $accountSwitcher;
/**
* The Linkchecker settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $linkcheckerSetting;
/**
* Number of items per batch.
*
* @var int
*/
protected $itemsPerBatch;
/**
* LinkStatusHandlerBase constructor.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueueFactory $queueFactory, EntityTypeManagerInterface $entityTypeManager, AccountSwitcherInterface $accountSwitcher, ImmutableConfig $linkcheckerSetting) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->queue = $queueFactory
->get('linkchecker_status_handle');
$this->entityTypeManager = $entityTypeManager;
$this->accountSwitcher = $accountSwitcher;
$this->linkcheckerSetting = $linkcheckerSetting;
$this->itemsPerBatch = 10;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('queue'), $container
->get('entity_type.manager'), $container
->get('account_switcher'), $container
->get('config.factory')
->get('linkchecker.settings'));
}
/**
* {@inheritdoc}
*/
public function queueItems(LinkCheckerLinkInterface $link, ResponseInterface $response) {
$items = $this
->getItems($link, $response);
if (empty($this->queue
->numberOfItems())) {
$this->queue
->createQueue();
}
foreach ($items as $item) {
$data = [];
$data['links'] = $item;
$data['response'] = $response;
$data['handler'] = $this
->getPluginId();
$this->queue
->createItem($data);
}
}
/**
* {@inheritdoc}
*/
public function handle(LinkCheckerLinkInterface $link, ResponseInterface $response) {
$this
->switchSession();
$entity = $link
->getParentEntity();
// Fields could be translatable.
// We should work with translation only.
if ($entity instanceof TranslatableInterface) {
if ($entity
->hasTranslation($link
->getParentEntityLangcode())) {
$entity = $entity
->getTranslation($link
->getParentEntityLangcode());
$this
->doHandle($link, $response, $entity);
}
}
else {
$this
->doHandle($link, $response, $entity);
}
$this
->switchSessionBack();
}
/**
* Handles a status code of link.
*
* @param \Drupal\linkchecker\LinkCheckerLinkInterface $link
* The link.
* @param \Psr\Http\Message\ResponseInterface $response
* The response of link checking.
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity with proper translation loaded.
*/
protected abstract function doHandle(LinkCheckerLinkInterface $link, ResponseInterface $response, FieldableEntityInterface $entity);
/**
* Gets list of items to queue.
*
* List should be like:
* [
* [
* 'entity_type' => 'node',
* 'entity_id' => 123,
* 'link_id' => 12
* ],
* ...
* [
* 'entity_type' => 'paragraph',
* 'entity_id' => 1234,
* 'link_id' => 123
* ],
* ]
*
* @param \Drupal\linkchecker\LinkCheckerLinkInterface $link
* The link.
* @param \Psr\Http\Message\ResponseInterface $response
* The response of link checking.
*
* @return array
* Array of items.
*/
protected function getItems(LinkCheckerLinkInterface $link, ResponseInterface $response) {
$linkStorage = $this->entityTypeManager
->getStorage($link
->getEntityTypeId());
$query = $linkStorage
->getQuery();
$query
->condition('urlhash', $link
->getHash());
$linkIds = $query
->execute();
return array_chunk($linkIds, $this->itemsPerBatch, TRUE);
}
/**
* Helper function to switch session.
*/
protected function switchSession() {
// Switch anonymous user to an admin.
$this->accountSwitcher
->switchTo(new UserSession([
'uid' => user_load_by_name($this->linkcheckerSetting
->get('error.impersonate_account')),
]));
}
/**
* Helper function to switch session back.
*/
protected function switchSessionBack() {
$this->accountSwitcher
->switchBack();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LinkStatusHandlerBase:: |
protected | property | The account switcher. | |
LinkStatusHandlerBase:: |
protected | property | The entity type manager. | |
LinkStatusHandlerBase:: |
protected | property | Number of items per batch. | |
LinkStatusHandlerBase:: |
protected | property | The Linkchecker settings. | |
LinkStatusHandlerBase:: |
protected | property | The queue. | |
LinkStatusHandlerBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
LinkStatusHandlerBase:: |
abstract protected | function | Handles a status code of link. | 2 |
LinkStatusHandlerBase:: |
protected | function | Gets list of items to queue. | 2 |
LinkStatusHandlerBase:: |
public | function |
Handles a status code of link. Overrides LinkStatusHandlerInterface:: |
|
LinkStatusHandlerBase:: |
public | function |
Creates a queue for handling. Overrides LinkStatusHandlerInterface:: |
|
LinkStatusHandlerBase:: |
protected | function | Helper function to switch session. | |
LinkStatusHandlerBase:: |
protected | function | Helper function to switch session back. | |
LinkStatusHandlerBase:: |
public | function |
LinkStatusHandlerBase constructor. Overrides PluginBase:: |
1 |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. |