public function QueryActivity::logActivity in Activity 8
Log actions in table activity.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity.
string $hook: Event when the log should be inserted.
File
- src/
QueryActivity.php, line 183
Class
- QueryActivity
- Provides queries for activity.
Namespace
Drupal\activityCode
public function logActivity(EntityInterface $entity, $hook) {
$entityNode = NULL;
$entityUser = NULL;
$entityComment = NULL;
$entityType = $entity
->getEntityTypeId();
switch ($entity
->getEntityTypeId()) {
case 'node':
$activityOption[] = $entity
->getType();
$nodeId = $entity
->get('nid')
->getValue();
$nid = $nodeId[0]['value'];
$userId = $entity
->get('uid')
->getValue();
$uid = $userId[0]['target_id'];
$entityNode = $entity;
$entityUser = User::load($uid);
$status = $entity
->get('status')
->getValue();
break;
case 'comment':
$nodeId = $entity
->get('entity_id')
->getValue();
$nid = $nodeId[0]['target_id'];
$entityNode = Node::load($nid);
$activityOption[] = $entityNode
->getType();
$entityComment = $entity;
$userId = $entity
->get('uid')
->getValue();
$uid = $userId[0]['target_id'];
$entityUser = User::load($uid);
$status = $entity
->get('status')
->getValue();
break;
case 'user':
$rolesOptions = $entity
->get('roles');
$activityOption = [
'0' => 'authenticated',
];
foreach ($rolesOptions as $key => $value) {
$role = $value
->getValue();
$activityOption[] = $role['target_id'];
}
$uid = $this->currentUser
->id();
$nid = NULL;
$entityUser = $entity;
$status = $entity
->get('status')
->getValue();
if ($status[0]['value'] == FALSE) {
$status[0]['value'] = 0;
}
elseif ($status[0]['value'] == TRUE) {
$status[0]['value'] = 1;
}
break;
default:
}
// Insert into activity table all actions.
$results = $this
->getMessage($hook);
if (!empty($results)) {
foreach ($results as $key => $value) {
$message = json_decode($value->message);
$types = $message->types;
if (empty($types)) {
$roles = $message->roles;
}
else {
$types = $message->types;
}
if (!empty($activityOption)) {
if (!empty($types)) {
foreach ($activityOption as $act => $activityValue) {
if (in_array($activityValue, $types)) {
$activityMessage = \Drupal::token()
->replace($message->message, [
'node' => $entityNode,
'user' => $entityUser,
'comment' => $entityComment,
]);
$this
->insertActivity($value->event_id, $entityType, $nid, $uid, $status[0]['value'], $activityMessage);
}
}
}
elseif (!empty($roles)) {
if (array_intersect($activityOption, $roles)) {
$activityMessage = \Drupal::token()
->replace($message->message, [
'node' => $entityNode,
'user' => $entityUser,
'comment' => $entityComment,
]);
$this
->insertActivity($value->event_id, $entityType, $nid, $uid, $status[0]['value'], $activityMessage);
}
}
}
}
}
}