Skip to content

Commit

Permalink
Merge pull request #11 from BurdaMagazinOrg/feature/DRPLDCX-57/remove…
Browse files Browse the repository at this point in the history
…-pubinfo-on-media-deletion

DRPLDCX-57 Remove pubinfo when media is deleted
  • Loading branch information
chrfritsch committed May 4, 2016
2 parents 2cd3fa2 + 016c45e commit 91d1200
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
6 changes: 6 additions & 0 deletions modules/dcx_integration_debug/src/MockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,11 @@ public function pubinfoOnPath($path) {
return [];
}

/**
* {{@inheritdoc}}
*/
public function removeAllUsage($dcx_id) {
dpm($dcx_id, __METHOD__);
}

}
23 changes: 20 additions & 3 deletions modules/dcx_track_media_usage/dcx_track_media_usage.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,36 @@ use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\field\Entity\FieldConfig;

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

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

/*
* Implement hook_ENTITY_TYPE_delete()-
*/
function dcx_track_media_usage_media_delete($entity) {
if ('image' !== $entity->bundle()) {
return;
}

$dcx_id = $entity->field_dcx_id->value;
try {
Drupal::service('dcx_integration.client')->removeAllUsage($dcx_id);
}
catch (\Exception $e) {
drupal_set_message($e->getMessage(), 'error');
}
}

/**
* Find media attached to this entity and emmit usage message to DC-X.
*/
Expand All @@ -25,7 +42,7 @@ function _dcx_track_media_usage_track_media_usage($entity) {
$usage = $discovery->discover($entity);

$url = $entity->toUrl()->getInternalPath();

$status = $entity->status->value;
try {
Drupal::service('dcx_integration.client')->trackUsage($usage, $url, $status);
Expand Down
13 changes: 12 additions & 1 deletion src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,18 @@ public function archiveArticle($url, $title, $text, $dcx_id);
*
* @param string $path canonical path (e.g. node/23)
*
* return array of array of pubinfo data keyed by DC-X document ID
* @return array of array of pubinfo data keyed by DC-X document ID
*/
public function pubinfoOnPath($path);


/**
* Removes all usage information about the given DC-X ID on the current site.
*
* The main reason for calling this would be deleteing the entity representin
* the given ID.
*
* @param string $dcx_id the DC-X document ID
*/
public function removeAllUsage($dcx_id);
}
26 changes: 25 additions & 1 deletion src/JsonClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,16 @@ public function pubinfoOnPath($path) {
return $pubinfo;
}

public function removePubinfos($pubinfos) {
/**
* Deletes the given pubinfo entries
*
* This just deletes. It does not make any sanity checks at all.
*
* @param array $pubinfos list of pubinfo entries as returned by DC-X.
*
* @throws \Exception
*/
protected function removePubinfos($pubinfos) {
foreach ($pubinfos as $data) {
$dcx_api_url = $data['_id_url'];
$http_status = $this->api_client->deleteObject($dcx_api_url, [], $response_body);
Expand All @@ -479,4 +488,19 @@ public function removePubinfos($pubinfos) {
}
}
}

/**
* {{@inheritdoc}}
*/
public function removeAllUsage($dcx_id) {
$document = $this->getJson($dcx_id);
$pubinfos = $document['_referenced']['dcx:pubinfo'];

foreach ($pubinfos as $key => $pubinfo) {
if ("dcxapi:tm_topic/" . $this->publication_id !== $pubinfo['properties']['publication_id']['_id']) {
unset($pubinfos[$key]);
}
}
$this->removePubinfos($pubinfos);
}
}

0 comments on commit 91d1200

Please sign in to comment.