Skip to content

Commit

Permalink
Merge pull request #6 from adrianodias8/#5_prevent_errors_if_items_do…
Browse files Browse the repository at this point in the history
…nt_exist

#5 prevent errors if items dont exist
  • Loading branch information
adrianodias8 authored Mar 31, 2020
2 parents 76048d9 + eeacbf5 commit 96f0da5
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ protected function getRelatedEntity(ContentEntityInterface $entity) {
case 'comment':
if (isset($this->pluginDefinition['credit_related'])) {
if ($this->pluginDefinition['credit_related'] == 'group') {
$group_contents = $this->entityTypeManager->getStorage('group_content')->loadByEntity($entity->getCommentedEntity());
$node = $entity->getCommentedEntity();
if (empty($node)) {
return FALSE;
}
$group_contents = $this->entityTypeManager->getStorage('group_content')->loadByEntity($node);
if ($group_content = reset($group_contents)) {
return $this->getGroup($group_content);
}
Expand All @@ -79,9 +83,17 @@ protected function getRelatedEntity(ContentEntityInterface $entity) {
}
}
if ($this->pluginDefinition['credit_related'] == 'group_content') {
$group_contents = $this->entityTypeManager->getStorage('group_content')->loadByEntity($entity->getCommentedEntity());
$group_content = reset($group_contents);
return $group_content;
$node = $entity->getCommentedEntity();
if (empty($node)) {
return FALSE;
}
$group_contents = $this->entityTypeManager->getStorage('group_content')->loadByEntity($node);
if ($group_content = reset($group_contents)) {
return $group_content;
}
else {
return FALSE;
}
}
}
break;
Expand All @@ -107,9 +119,15 @@ protected function getRelatedEntity(ContentEntityInterface $entity) {
* Group Content entity.
*/
protected function getGroup(GroupContentInterface $group_content) {
// Since we have a group_conent we can get the group.
// Since we have a group_content we can get the group.
$group = $group_content->getGroup();

// Prevent further execution if no group was found.
if (empty($group)) {
\Drupal::logger('entity_activity_tracker')->error($this->t("Couldn't find Group!"));
return FALSE;
}

// Now we must find a tracker that matches the group
// since a tracker is needed to create activity records.
$properties = [
Expand All @@ -123,7 +141,7 @@ protected function getGroup(GroupContentInterface $group_content) {
// Do something if there is a Tracker for group where content was created.
if ($group_tracker) {
// I NEED TO THINK HOW TO HANDLE MULTIPLE.
// SINCE WE DONT ALLOW A NODE BE PART OF 2 DIFERENT GROUPS ITS OK FOR NOW.
// SINCE WE DONT ALLOW A NODE BE PART OF 2 DIFFERENT GROUPS ITS OK FOR NOW.
return $group;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ protected function getRelatedEntity(ContentEntityInterface $entity) {
if ($group_content_related_entity = parent::getRelatedEntity($entity)) {
$group = $this->getGroup($group_content_related_entity);

// Prevent further execution if no group was found.
if (empty($group)) {

\Drupal::logger('entity_activity_tracker')->error($this->t("Couldn't find Group!"));
return FALSE;
}

// Get the membership.
$author_memberships = $this->entityTypeManager->getStorage('group_content')->loadByEntity($entity->getOwner());
foreach ($author_memberships as $author_membership) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public function processActivity(Event $event) {
case ActivityEventInterface::ENTITY_DELETE:
/** @var \Drupal\entity_activity_tracker\ActivityRecord $activity_record */
$activity_record = $this->activityRecordStorage->getActivityRecordByEntity($event->getEntity());
$this->activityRecordStorage->deleteActivityRecord($activity_record);
if ($activity_record) {
$this->activityRecordStorage->deleteActivityRecord($activity_record);
}
break;

case ActivityEventInterface::TRACKER_DELETE:
Expand Down
2 changes: 1 addition & 1 deletion src/EventSubscriber/ActivitySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function scheduleDecay(CronEvent $event) {
public function dispatchActivityEvent(Event $event) {
// TODO: IMPROVE THIS FIRST CONDITION!!
if (in_array($event->getEntity()->getEntityTypeId(), EntityActivityTrackerInterface::ALLOWED_ENTITY_TYPES)) {
// Dispatch curresponding activity event.
// Dispatch corresponding activity event.
$this->activityEventDispatcher->dispatchActivityEvent($event);

// TODO: Think a way to hook this. to let other modules play.
Expand Down
18 changes: 17 additions & 1 deletion src/Plugin/ActivityProcessor/EntityCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ public function processActivity(Event $event) {
case ActivityEventInterface::ENTITY_DELETE:
/** @var \Drupal\entity_activity_tracker\ActivityRecord $activity_record */
$activity_record = $this->activityRecordStorage->getActivityRecordByEntity($event->getEntity());
$this->activityRecordStorage->deleteActivityRecord($activity_record);
if ($activity_record) {
$this->activityRecordStorage->deleteActivityRecord($activity_record);
}
break;

case ActivityEventInterface::TRACKER_DELETE:
Expand Down Expand Up @@ -160,4 +162,18 @@ protected function getExistingEntities(EntityActivityTrackerInterface $tracker)

}

/**
* {@inheritdoc}
*/
public function canProcess(Event $event) {
// This should change since doesn't make sense to store Entity in event to then
// load it again.
$entity = $event->getEntity();
$exists = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->load($entity->id());
if (empty($exists) && $event->getDispatcherType() == ActivityEventInterface::ENTITY_INSERT) {
return ActivityProcessorInterface::SKIP;
}
return ActivityProcessorInterface::PROCESS;
}

}
14 changes: 14 additions & 0 deletions src/Plugin/ActivityProcessor/EntityEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,18 @@ public function processActivity(Event $event) {
}
}

/**
* {@inheritdoc}
*/
public function canProcess(Event $event) {
// This should change since doesn't make sense to store Entity in event to then
// load it again.
$entity = $event->getEntity();
$exists = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->load($entity->id());
if (empty($exists)) {
return ActivityProcessorInterface::SKIP;
}
return ActivityProcessorInterface::PROCESS;
}

}
2 changes: 1 addition & 1 deletion src/Plugin/ActivityProcessorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function processActivity(Event $event) {
* Let plugins decide if can process.
*/
public function canProcess(Event $event) {
// By default we will tell to ActivityProcessorQueue to allways process.
// By default we will tell to ActivityProcessorQueue to always process.
return ActivityProcessorInterface::PROCESS;
}

Expand Down
31 changes: 22 additions & 9 deletions src/Plugin/ActivityProcessorCreditRelatedBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\entity_activity_tracker\Plugin;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\entity_activity_tracker\Event\ActivityEventInterface;
use Drupal\entity_activity_tracker\Event\EntityActivityInsertEvent;
use Symfony\Component\EventDispatcher\Event;

Expand All @@ -28,19 +29,17 @@ public function getSummary() {
*/
public function processActivity(Event $event) {
$dispatcher_type = $event->getDispatcherType();
switch ($dispatcher_type) {
case EntityActivityInsertEvent::ENTITY_INSERT:

/** @var \Drupal\Core\Entity\ContentEntityInterface $entity*/
switch ($dispatcher_type) {
case ActivityEventInterface::ENTITY_INSERT:
$entity = $event->getEntity();

// Get related entity.
if ($related_entity = $this->getRelatedEntity($entity)) {
$this->creditRelated($related_entity);
}
break;

case EntityActivityInsertEvent::TRACKER_CREATE:
case ActivityEventInterface::TRACKER_CREATE:
// Iterate all already existing entities and credit related.
foreach ($this->getExistingEntities($event->getTracker()) as $existing_entity) {
// Get related entity.
Expand All @@ -49,7 +48,16 @@ public function processActivity(Event $event) {
}
}
break;

case ActivityEventInterface::TRACKER_DELETE:
$tracker = $event->getTracker();
// Get ActivityRecords from this tracker.
foreach ($this->activityRecordStorage->getActivityRecords($tracker->getTargetEntityType(), $tracker->getTargetEntityBundle()) as $activity_record) {
$this->activityRecordStorage->deleteActivityRecord($activity_record);
}
break;
}

}

/**
Expand Down Expand Up @@ -87,7 +95,7 @@ public function canProcess(Event $event) {

// Get related entity.
if ($related_entity = $this->getRelatedEntity($existing_entity)) {
// If we get a related entity we load the actibity record.
// If we get a related entity we load the activity record.
// This will return false if related record doesn't exit.
$related_records[] = $this->activityRecordStorage->getActivityRecordByEntity($related_entity);
}
Expand All @@ -98,14 +106,17 @@ public function canProcess(Event $event) {
return ActivityProcessorInterface::SKIP;
}
elseif (!in_array(FALSE, $related_records, TRUE)) {
// All related records needed exitst.
// All related records needed exist.
return ActivityProcessorInterface::PROCESS;
}
else {
// Not all related records needed exitst.
// Not all related records needed exist.
return ActivityProcessorInterface::SCHEDULE;
}
break;

default:
return ActivityProcessorInterface::PROCESS;
}
}

Expand All @@ -124,7 +135,9 @@ protected function getRelatedEntity(ContentEntityInterface $entity) {
return $entity->getCommentedEntity();
}
if ($this->pluginDefinition['credit_related'] == 'user') {
return $entity->getOwner();
$user = $entity->getOwner();
// Prevent schedule for anonymous users.
return $user->id() != 0 ? $user : FALSE;
}
}
break;
Expand Down

0 comments on commit 96f0da5

Please sign in to comment.