Skip to content
Snippets Groups Projects
Commit 2a860199 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3339992: How to configure WebRanges on headless sites?

parent 3050fa44
No related branches found
No related tags found
1 merge request!14Issue #3339992: How to configure WebRanges on headless sites?
......@@ -5,10 +5,16 @@ Install this module to add VG Wort's tracking 1x1 pixel to content entity types
of your choice.
## Excluding an entity from VG Wort
If you've configured nodes to have the VG Wort counter ID you can implement
If you've configured entities to have the VG Wort counter ID you can implement
`hook_vgwort_enable_for_entity()` to exclude specific entities from VG Wort.
See [vgwort.api.php](vgwort.api.php) for more information.
## Altering information sent to VG Wort
If you need to alter the information sent to VGWort you can implement
`hook_vgwort_new_message_alter()`. This hook will allow you to alter the URL and
legal flags for an entity. This is useful for decoupled sites. See
[vgwort.api.php](vgwort.api.php) for more information.
## Using an entity reference field to list participants
The module provides the ability to use any entity reference field to determine
VG Wort participant info. For example, if you have an author node type and
......
......@@ -5,6 +5,7 @@ namespace Drupal\vgwort;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\vgwort\Api\MessageText;
use Drupal\vgwort\Api\NewMessage;
......@@ -35,6 +36,11 @@ class MessageGenerator {
*/
protected $config;
/**
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a MessageGenerator object.
*
......@@ -46,12 +52,15 @@ class MessageGenerator {
* The renderer service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
*/
public function __construct(ParticipantListManager $participantListManager, EntityTypeManagerInterface $entityTypeManager, RendererInterface $renderer, ConfigFactoryInterface $configFactory) {
public function __construct(ParticipantListManager $participantListManager, EntityTypeManagerInterface $entityTypeManager, RendererInterface $renderer, ConfigFactoryInterface $configFactory, ModuleHandlerInterface $moduleHandler) {
$this->participantListManager = $participantListManager;
$this->entityTypeManager = $entityTypeManager;
$this->renderer = $renderer;
$this->config = $configFactory->get('vgwort.settings');
$this->moduleHandler = $moduleHandler;
}
/**
......@@ -82,24 +91,29 @@ class MessageGenerator {
(string) $this->renderer->renderPlain($build)
);
$participants = $this->participantListManager->getParticipants($entity);
// @todo How to set this correctly when the backend and frontend are
// decoupled.
$webranges = [new Webrange([$entity->toUrl()->setAbsolute()->toString()])];
$legal_rights = $this->config->get('legal_rights');
// @todo How to set other_public_communication and withoutOwnParticipation
// correctly.
// All modules to alter some of the data we send to VG Wort.
$alter_data = [
'webranges' => $webranges,
'legal_rights' => $legal_rights,
'without_own_participation' => FALSE,
];
$this->moduleHandler->alter('vgwort_new_message', $alter_data, $entity);
return new NewMessage(
$vgwort_id,
$text,
$participants,
$webranges,
$legal_rights['distribution'],
$legal_rights['public_access'],
$legal_rights['reproduction'],
$legal_rights['declaration_of_granting'],
$legal_rights['other_public_communication'],
$alter_data['webranges'],
$alter_data['legal_rights']['distribution'],
$alter_data['legal_rights']['public_access'],
$alter_data['legal_rights']['reproduction'],
$alter_data['legal_rights']['declaration_of_granting'],
$alter_data['legal_rights']['other_public_communication'],
$alter_data['without_own_participation']
);
}
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider;
use Drupal\vgwort\Api\Webrange;
/**
* Implements hook_vgwort_enable_for_entity().
......@@ -30,3 +31,14 @@ function vgwort_test_entity_type_alter(array &$entity_types) {
}
}
}
/**
* Implements hook_vgwort_new_message_alter().
*/
function vgwort_test_vgwort_new_message_alter(array &$data, EntityInterface $entity) {
if (\Drupal::state()->get('vgwort_test_vgwort_new_message_alter', FALSE)) {
$data['without_own_participation'] = TRUE;
$data['webranges'] = [new Webrange(['http://decoupled_site.example.com/' . $entity->uuid()])];
$data['legal_rights']['other_public_communication'] = TRUE;
}
}
......@@ -84,6 +84,66 @@ class MessageGeneratorTest extends VgWortKernelTestBase {
],
"withoutOwnParticipation": false
}
JSON;
$this->assertSame($expected_message, $this->jsonEncode($message));
}
/**
* @see \hook_vgwort_new_message_alter()
*/
public function testVgwortNewMessageAlter() {
$this->enableModules(['vgwort_test']);
$this->container->get('state')->set('vgwort_test_vgwort_new_message_alter', TRUE);
$entity_type = 'entity_test';
$entity = $this->container->get('entity_type.manager')
->getStorage($entity_type)
->create([
'vgwort_test' => [
'card_number' => '123123123',
'firstname' => 'Bob',
'surname' => 'Jones',
'agency_abbr' => '',
],
'text' => 'Some text',
'name' => 'A title',
]);
$entity->save();
$message = $this->container->get('vgwort.message_generator')->entityToNewMessage($entity);
$expected_message = <<<JSON
{
"distributionRight": true,
"messagetext": {
"lyric": false,
"shorttext": "A title",
"text": {
"plainText": "ZnVsbCB8IEEgdGl0bGVBIHRpdGxlU29tZSB0ZXh0"
}
},
"otherRightsOfPublicReproduction": true,
"participants": [
{
"cardNumber": 123123123,
"firstName": "Bob",
"surName": "Jones",
"involvement": "AUTHOR"
}
],
"privateidentificationid": "vgzm.123456-{$entity->uuid()}",
"publicAccessRight": true,
"reproductionRight": true,
"rightsGrantedConfirmation": true,
"webranges": [
{
"urls": [
"http:\/\/decoupled_site.example.com\/{$entity->uuid()}"
]
}
],
"withoutOwnParticipation": true
}
JSON;
$this->assertSame($expected_message, $this->jsonEncode($message));
......
......@@ -22,3 +22,24 @@
function hook_vgwort_enable_for_entity(\Drupal\Core\Entity\EntityInterface $entity): bool {
return $entity->bundle() !== 'not_my_content';
}
/**
* Alters the information sent about an entity to VG Wort.
*
* @param array $data
* Data to alter. This consists of the following data with the keys:
* - webranges: An array of \Drupal\vgwort\Api\Webrange objects. Defaults to
* a webrange containing the canonical URL of the entity.
* - legal_rights: An array of booleans with the same structure as
* vgwort.settings:legal_rights. Defaults to the values set in
* configuration.
* - without_own_participation: A boolean that indicates whether the publisher
* is involved. Defaults FALSE that indicates the publisher is involved.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that is being used to generate the data to send to VG Wort.
*/
function hook_vgwort_new_message_alter(array &$data, \Drupal\Core\Entity\EntityInterface $entity): void {
$data['without_own_participation'] = TRUE;
$data['webranges'] = [new \Drupal\vgwort\Api\Webrange(['http://decoupled_site.com/' . $entity->id()])];
$data['legal_rights']['other_public_communication'] = TRUE;
}
......@@ -9,7 +9,7 @@ services:
- { name: event_subscriber }
vgwort.message_generator:
class: Drupal\vgwort\MessageGenerator
arguments: ['@vgwort.participant_list_manager', '@entity_type.manager', '@renderer', '@config.factory']
arguments: ['@vgwort.participant_list_manager', '@entity_type.manager', '@renderer', '@config.factory', '@module_handler']
vgwort.entity_queuer:
class: Drupal\vgwort\EntityQueuer
arguments: ['@config.factory', '@vgwort.entity_job_mapper']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment