ForumTopicHandler.php in Opigno forum 3.x
File
src/ForumTopicHandler.php
View source
<?php
namespace Drupal\opigno_forum;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ForumTopicHandler implements ContainerInjectionInterface {
protected $entityTypeManager;
protected $entityFieldManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_field.manager'));
}
public static function get() {
$handler = \Drupal::classResolver()
->getInstanceFromDefinition(static::class);
return $handler;
}
public function onNodeInsert(NodeInterface $node) {
$node_type_id = $node
->bundle();
if ($this
->isForumTopicType($node_type_id) && $this
->isLearningPathContent($node_type_id) && ($gid = $this
->getForumTopicGroupId($node))) {
$group = $this->entityTypeManager
->getStorage('group')
->load($gid);
drupal_register_shutdown_function(function () use ($group, $node) {
$group
->addContent($node, 'group_node:' . $node
->bundle());
});
}
}
public function isLearningPathContent($node_type_id) {
try {
$group_type = $this->entityTypeManager
->getStorage('group_type')
->load('learning_path');
$group_type
->getContentPlugin('group_node:' . $node_type_id);
return TRUE;
} catch (PluginNotFoundException $e) {
return FALSE;
}
}
public function isForumTopicType($node_type_id) {
$definitions = $this->entityFieldManager
->getFieldDefinitions('node', $node_type_id);
return isset($definitions['taxonomy_forums']);
}
public function getForumTopicGroupId(NodeInterface $topic) {
$tid = $topic
->get('taxonomy_forums')->target_id;
$ids = $this->entityTypeManager
->getStorage('group')
->getQuery()
->condition('field_learning_path_enable_forum', 1)
->condition('field_learning_path_forum', $tid)
->accessCheck(FALSE)
->execute();
return $ids ? reset($ids) : NULL;
}
public function getForumTopicTypeIds() {
$topic_type_ids = [];
$result = $this->entityTypeManager
->getStorage('group')
->getAggregateQuery()
->groupBy('field_learning_path_forum')
->aggregate('field_learning_path_enable_forum', 'COUNT')
->condition('field_learning_path_enable_forum', 1)
->accessCheck(FALSE)
->execute();
$tids = array_map(function ($record) {
return $record['field_learning_path_forum_target_id'];
}, $result);
if ($tids) {
$result = $this->entityTypeManager
->getStorage('node')
->getAggregateQuery()
->groupBy('type')
->aggregate('nid', 'COUNT')
->condition('taxonomy_forums', $tids, 'IN')
->accessCheck(FALSE)
->execute();
$topic_type_ids = array_map(function ($record) {
return $record['type'];
}, $result);
}
return $topic_type_ids;
}
}