Skip to content
Snippets Groups Projects
Commit bc8b841a authored by Eirik Morland's avatar Eirik Morland
Browse files

Issue #3294846 by devkinetic, eiriksm, imclean: Expose the url to the entity for Views

parent 134a9e66
Branches
Tags
3 merge requests!93Issue #3294846 by devkinetic, eiriksm, imclean: Expose the url to the entity for Views,!75Resolve #3313343 "Disable cron 2.0.x",!16Issue #3247070: TypeError in LinkStatusHandle.php
Pipeline #384188 passed with warnings
......@@ -22,7 +22,8 @@
"drupal/core": "^9.4 || ^10.0 || ^11"
},
"require-dev": {
"drupal/redirect": "^1.8"
"drupal/redirect": "^1.8",
"drupal/paragraphs": "*"
},
"extra": {
"drush": {
......
......@@ -16,4 +16,11 @@ function linkchecker_views_data_alter(array &$data) {
'id' => 'linkcheckerlink_page_entity_label',
],
];
$data['linkchecker_link']['linkcheckerlink_page_entity_link'] = [
'field' => [
'title' => t('Page entity link'),
'help' => t('The link of the entity that is related to the file.'),
'id' => 'linkcheckerlink_page_entity_link',
],
];
}
<?php
namespace Drupal\linkchecker\Plugin\views\field;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\linkchecker\LinkCheckerLinkInterface;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\views\Plugin\views\field\EntityLink;
use Drupal\views\ResultRow;
/**
* Field handler that builds the page entity link for the linkchecker_link.
*
* @ingroup views_field_handlers
*
* @ViewsField("linkcheckerlink_page_entity_link")
*/
class LinkcheckerLinkPageEntityLink extends EntityLink {
/**
* {@inheritdoc}
*/
public function getEntity(ResultRow $values) {
$linkchecker_link = parent::getEntity($values);
if (!$linkchecker_link instanceof LinkCheckerLinkInterface) {
return NULL;
}
if (!$linkchecker_link->hasField('entity_id')) {
return NULL;
}
if ($linkchecker_link->get('entity_id')->isEmpty()) {
return NULL;
}
$linked_entity = $linkchecker_link->get('entity_id')->entity;
if (!$linked_entity instanceof EntityInterface) {
return NULL;
}
while ($linked_entity instanceof ParagraphInterface && $linked_entity->getParentEntity() !== NULL) {
$linked_entity = $linked_entity->getParentEntity();
}
try {
$url = $linked_entity->toUrl();
}
catch (UndefinedLinkTemplateException | EntityMalformedException $e) {
return NULL;
}
return $linked_entity;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment