Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/drpldcx 23/pubinfo #2

Merged
merged 9 commits into from
Apr 19, 2016
1 change: 1 addition & 0 deletions dcx_integration.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ description: 'Integration of DC-X digital asset management.'

version: '0'
core: '8.x'
package: dcx

56 changes: 0 additions & 56 deletions dcx_integration.module

This file was deleted.

3 changes: 2 additions & 1 deletion modules/dcx_article_archive/dcx_article_archive.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: DC-X Article Archive
type: module
description: Archive Articles to DC-X
core: 8.x
package: Custom
package: dcx

dependencies:
- dcx_integration
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ description: 'Debugging helper for DC-X integration. Mocking DC-X the service.'

version: '0'
core: '8.x'
package: dcx

dependencies:
- dcx_integration
6 changes: 3 additions & 3 deletions modules/dcx_integration_debug/src/MockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ protected function buildStoryAsset($url) {
return new Article($data);
}

public function trackUsage($id, $url) {
print_r("Media $id used on URL {" . $url . "}");
public function trackUsage($dcx_ids, $url, $published) {
dpm("Media " . print_r($dcx_ids, 1) . " used on URL {" . $url . "}");
}

/**
Expand All @@ -79,7 +79,7 @@ public function archiveArticle($url, $title, $text, $dcx_id) {
if (!$dcx_id) {
$dcx_id = "dcxapi:document/doc__mocked__" . rand(10000000000, 99999999999);
}

return $dcx_id;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
uuid: 51fbed76-12e5-4949-bd09-9d4ebd12234e
langcode: en
status: true
dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
uuid: efe612b1-fe02-45af-8000-e9b7a348e84c
langcode: en
status: true
dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
uuid: 89835a1f-04a9-42d0-a003-3d4439a5c09f
langcode: en
status: true
dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
uuid: ad36bb56-9d9e-414d-bc31-49c1a4c733e2
langcode: en
status: true
dependencies:
Expand Down
2 changes: 2 additions & 0 deletions modules/dcx_migration/dcx_migration.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ description: 'Migration of data from DC-X digital asset management.'

version: '0'
core: '8.x'
package: dcx

dependencies:
- dcx_integration
- migrate
- migrate_plus #MigratePlusEvents::PREPARE_ROW
- media_entity
7 changes: 7 additions & 0 deletions modules/dcx_track_media_usage/dcx_track_media_usage.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: DC-X Track Media Usage
type: module
description: Provides usage tracking of DC-X media an nodes
core: 8.x
package: dcx
dependencies:
- dcx_integration
137 changes: 137 additions & 0 deletions modules/dcx_track_media_usage/dcx_track_media_usage.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\field\Entity\FieldConfig;

/**
* Implement hook_entity_insert()-
*/
function dcx_track_media_usage_node_insert($entity) {
_dcx_track_media_usage_track_media_usage($entity);
}

/**
* Implement hook_entity_insert()-
*/
function dcx_track_media_usage_node_update($entity) {
_dcx_track_media_usage_track_media_usage($entity);
}

/**
* Find media attached to this entity and emmit usage message to DC-X.
*/
function _dcx_track_media_usage_track_media_usage($entity) {
// If this entity is not fieldable, it's non of our business.
if (! $entity instanceof FieldableEntityInterface) {
return;
}

// If this entity is an Image itself, we're not going to notify anything.
if ('media' === $entity->getEntityTypeId() && 'image' === $entity->bundle()) {
return;
}

$usage = _dcx_track_media_collect_usage_on_entity_reference_fields($entity);
$usage += _dcx_track_media_collect_usage_on_paragraphs($entity);

$url = $entity->toUrl()->toString();
$status = $entity->status->value;
try {
Drupal::service('dcx_integration.client')->trackUsage($usage, $url, $status);
} catch (\Exception $e) {
drupal_set_message($e->getMessage(), 'error');
}
}

/**
* Collect media:image entities referenced by entity reference fields on the
* given entity.
*
* @param type $entity
* @return array $usage list of DC-X IDs keyed by DC-X IDs.
*/
function _dcx_track_media_collect_usage_on_entity_reference_fields($entity) {

$usage = [];
// Iterate over the field definition of the given entitiy
foreach ($entity->getFieldDefinitions() as $definition) {
// Fields have FieldConfig. Let's assume our media is referenced within a
// field
if (! $definition instanceof FieldConfig) {
continue;
}
// Only care about entity reference fields
if ('entity_reference' !== $definition->getType()) { continue; }
$settings = $definition->getSettings();

// We can't be sure that a target type is defined. Deal with it.
$target_type = isset($settings['target_type'])?$settings['target_type']:NULL;

// Only care about field referencing media
if ('media' !== $target_type) { continue; }

$target_bundles = $settings['handler_settings']['target_bundles'];

// Only care about refs allowing images
if (! in_array('image', $target_bundles)) { continue; }

$field = $definition->getName();

// Don't care about empty reference fields;
if (empty($entity->$field->target_id)) { continue; }

$referenced_entities = $entity->$field->referencedEntities();
foreach ($referenced_entities as $referenced_entity) {
// Only care about image media
if ('image' !== $referenced_entity->bundle()) { continue; }

$dcx_id = $referenced_entity->field_dcx_id->value;

if (empty($dcx_id)) {
// @TODO This must not happen by contract. How do we deal if it happens?
throw \Exception(t('Media image %id has no DC-X ID', ['%id' => $referenced_entity->id()]));
}
$usage[$dcx_id] = $dcx_id;
}

}
return $usage;
}

/**
* Collect media:image entities referenced by paragraphs fields on the
* given entity.
*
* @param type $entity
* @return array $usage list of DC-X IDs keyed by DC-X IDs.
*/
function _dcx_track_media_collect_usage_on_paragraphs($entity) {
$usage = [];

dpm($entity);

foreach ($entity->getFieldDefinitions() as $definition) {

// Paragraphes are stored in a proper field with FieldConfig
if (! $definition instanceof FieldConfig) {
continue;
}

// Only care about entity_reference_revisions, which is the field type of
// paragraphs
if ('entity_reference_revisions' !== $definition->getType()) { continue; }

$settings = $definition->getSettings();

$target_type = isset($settings['target_type'])?$settings['target_type']:NULL;

// Only care about field referencing paragraphs
if ('paragraph' !== $target_type) { continue; }

$field = $definition->getName();

dpm($field);
}

return $usage;
}
15 changes: 14 additions & 1 deletion src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ interface ClientInterface {

public function getObject($id);

public function trackUsage($id, $url);
/**
* Track usage of DC-X Documents on the given URL.
*
* The given URL should be expanded to the appropriate public absolute URL.
*
* @param array $dcx_ids List of DC-X document IDs.
* @param string $url relative canonical URL where the documents are used.
* @param bool $published status of the given URL
*
* @throws \Exception if something is going wrong.
*/
public function trackUsage($dcx_ids, $url, $published);

/**
* Archive an article.
Expand All @@ -29,6 +40,8 @@ public function trackUsage($id, $url);
*
* @return int
* The DC-X document ID of the article
*
* @throws \Exception if something is going wrong.
*/
public function archiveArticle($url, $title, $text, $dcx_id);
}
6 changes: 3 additions & 3 deletions src/Controller/DcxDebugController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\dcx_integration\JsonClient;
use Drupal\dcx_integration\ClientInterface;

/**
* Class DcxDebugController.
Expand All @@ -28,7 +28,7 @@ class DcxDebugController extends ControllerBase {
/**
* {@inheritdoc}
*/
public function __construct(JsonClient $dcx_integration_client) {
public function __construct(ClientInterface $dcx_integration_client) {
$this->dcx_integration_client = $dcx_integration_client;
}

Expand Down Expand Up @@ -84,7 +84,7 @@ public function archive() {
catch (\Exception $e) {
dpm($e);
}

return [
'#type' => 'markup',
'#markup' => __METHOD__ . " " . $dcx_id,
Expand Down
30 changes: 28 additions & 2 deletions src/Form/JsonClientSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#title' => $this->t('URL'),
'#maxlength' => 64,
'#size' => 64,
'#required' => TRUE,
'#default_value' => $config->get('url'),
];
$form['username'] = [
Expand All @@ -53,12 +54,30 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#default_value' => $config->get('username'),
];
$form['password'] = [
'#type' => 'textfield',
'#type' => 'password',
'#title' => $this->t('Password'),
'#maxlength' => 64,
'#size' => 64,
'#default_value' => $config->get('password'),
];
$form['publication'] = [
'#type' => 'textfield',
'#title' => $this->t('Publication'),
'#maxlength' => 64,
'#size' => 64,
'#required' => TRUE,
'#default_value' => $config->get('publication'),
'#description' => $this->t('Machine name of the publication (this website) in DC-X, e.g "publication-freundin".')
];
$form['frontendurl'] = [
'#type' => 'textfield',
'#title' => $this->t('Frontend-URL'),
'#maxlength' => 64,
'#size' => 64,
'#default_value' => $config->get('frontendurl'),
'#description' => $this->t('The public facing frontpage URL of this website, e.g "http://www.bunte.de". If there are no alternative backend urls (like e.g. http://redaktion.bunte.de) you may leave this blank.'),
];

return parent::buildForm($form, $form_state);
}

Expand All @@ -75,10 +94,17 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);

$password = $form_state->getValue('password');
if (empty($password)) {
$password = $config = $this->config('dcx_integration.jsonclientsettings')->get('password');
}

$this->config('dcx_integration.jsonclientsettings')
->set('url', $form_state->getValue('url'))
->set('username', $form_state->getValue('username'))
->set('password', $form_state->getValue('password'))
->set('password', $password)
->set('frontendurl', trim($form_state->getValue('frontendurl')))
->set('publication', trim($form_state->getValue('publication')))
->save();
}

Expand Down
Loading