Commit 88b6c938 authored by alexpott's avatar alexpott

Issue #1951268 by larowlan, nick_schuch, jibran, dawehner, alexpott,...

Issue #1951268 by larowlan, nick_schuch, jibran, dawehner, alexpott, tim.plunkett: Convert /forum and /forum/% to new routing system, remove forum_forum_load(), forum_get_topics(), create new Forum service.
parent fe0fd73a
id: taxonomy_term.forums.default
uuid: adaef6a9-8dc0-4f2e-9858-88daac440aa9
targetEntityType: taxonomy_term
bundle: forums
mode: default
content:
description:
weight: '0'
visible: '1'
id: taxonomy_term.forums.default
uuid: c8eab085-8fd3-4545-8600-e13b7d8bb9c4
targetEntityType: taxonomy_term
bundle: forums
mode: default
content:
name:
weight: '-5'
visible: '1'
description:
weight: '0'
visible: '1'
id: taxonomy_term.forum_container
uuid: babf2ba1-505f-4c71-8a07-7be19f4fb9f3
status: '1'
langcode: en
name: forum_container
type: list_boolean
settings:
allowed_values:
- ''
- ''
allowed_values_function: ''
module: options
active: '1'
entity_type: taxonomy_term
locked: '1'
cardinality: '1'
translatable: '0'
indexes: { }
id: taxonomy_term.forums.forum_container
uuid: 8421d585-f6ef-4209-ad00-cfb30a1ab075
status: '1'
langcode: en
field_uuid: babf2ba1-505f-4c71-8a07-7be19f4fb9f3
entity_type: taxonomy_term
bundle: forums
label: Container
description: ''
required: '1'
default_value:
-
value: '0'
default_value_function: ''
settings: { }
field_type: list_boolean
......@@ -3,7 +3,6 @@ block:
limit: '5'
new:
limit: '5'
containers: []
topics:
hot_threshold: '15'
order: '1'
......
......@@ -22,12 +22,6 @@ forum.settings:
limit:
type: integer
label: 'New forum Count'
containers:
type: sequence
label: 'Containers to group related forums'
sequence:
- type: integer
label: 'Taxonomy Term ID'
topics:
type: mapping
label: 'Forum topics block'
......
......@@ -32,7 +32,7 @@ function forum_overview($form, &$form_state) {
$term = $form['terms'][$key]['#term'];
$form['terms'][$key]['term']['#href'] = 'forum/' . $term->id();
unset($form['terms'][$key]['operations']['#links']['delete']);
if (in_array($form['terms'][$key]['#term']->id(), $config->get('containers'))) {
if (!empty($term->forum_container->value)) {
$form['terms'][$key]['operations']['#links']['edit']['title'] = t('edit container');
$form['terms'][$key]['operations']['#links']['edit']['href'] = 'admin/structure/forum/edit/container/' . $term->id();
// We don't want the redirect from the link so we can redirect the
......
......@@ -6,6 +6,7 @@ dependencies:
- history
- taxonomy
- comment
- options
package: Core
version: VERSION
core: 8.x
......
......@@ -5,6 +5,8 @@
* Install, update, and uninstall functions for the Forum module.
*/
use Drupal\Core\Language\Language;
/**
* Implements hook_install().
*/
......@@ -73,6 +75,7 @@ function forum_enable() {
'description' => '',
'parent' => array(0),
'vid' => $vocabulary->id(),
'forum_container' => 0,
));
$term->save();
......@@ -108,6 +111,18 @@ function forum_enable() {
}
}
/**
* Implements hook_modules_preinstall().
*/
function forum_modules_preinstall($modules) {
$list_boolean = Drupal::service('plugin.manager.entity.field.field_type')->getDefinition('list_boolean');
if (empty($list_boolean) && in_array('forum', $modules)) {
// Make sure that the list_boolean field type is available before our
// default config is installed.
field_info_cache_clear();
}
}
/**
* Implements hook_uninstall().
*/
......@@ -119,8 +134,15 @@ function forum_uninstall() {
$field->delete();
}
// Purge field data now to allow taxonomy module to be uninstalled
// if this is the only field remaining.
if ($field = field_info_field('taxonomy_term', 'forum_container')) {
$field->delete();
}
// Purge field data now to allow taxonomy and options module to be uninstalled
// if this is the only field remaining. We need to run it twice because
// field_purge_batch() will not remove the instance and the field in the same
// pass.
field_purge_batch(10);
field_purge_batch(10);
// Allow to delete a forum's node type.
$locked = Drupal::state()->get('node.type.locked');
......@@ -261,13 +283,131 @@ function forum_update_last_removed() {
* @ingroup config_upgrade
*/
function forum_update_8000() {
$map = db_query('SELECT vid, machine_name FROM {taxonomy_vocabulary}')->fetchAllKeyed();
$forum_vid = update_variable_get('forum_nav_vocabulary', FALSE);
if (!empty($map[$forum_vid])) {
// Update the variable to reference the machine name instead of the vid.
update_variable_set('forum_nav_vocabulary', $map[$forum_vid]);
}
update_variables_to_config('forum.settings', array(
'forum_hot_topic' => 'topics.hot_threshold',
'forum_per_page' => 'topics.page_limit',
'forum_order' => 'topics.order',
'forum_nav_vocabulary' => 'vocabulary',
'forum_containers' => 'containers',
'forum_block_num_active' => 'block.active.limit',
'forum_block_num_new' => 'block.new.limit',
));
}
/**
* Implements hook_update_dependencies().
*/
function forum_update_dependencies() {
// Convert containers to field after the fields and instances are converted to
// ConfigEntities.
$dependencies['forum'][8001] = array(
'field' => 8003,
'taxonomy' => 8007,
);
return $dependencies;
}
/**
* Adds the forum_container field and copies the values over.
*/
function forum_update_8001() {
$vocabulary = config('forum.settings')->get('vocabulary');
// Create the field and instance.
$field = array(
'id' => 'taxonomy_term.forum_container',
'name' => 'forum_container',
'entity_type' => 'taxonomy_term',
'module' => 'options',
'type' => 'list_boolean',
'cardinality' => 1,
'locked' => TRUE,
'indexes' => array(),
'settings' => array(
'allowed_values' => array('', ''),
'allowed_values_function' => '',
),
'schema' => array(
'columns' => array(
'value' => array(
'type' => 'int',
'not null' => FALSE,
),
),
'indexes' => array(),
'foreign keys' => array(),
),
);
_update_8003_field_create_field($field);
$instance = array(
'id' => 'taxonomy_term.' . $vocabulary . '.forum_container',
'entity_type' => 'taxonomy_term',
'label' => 'Container',
'bundle' => $vocabulary,
'description' => '',
'required' => TRUE,
'settings' => array(),
'default_value' => array('value' => 0),
);
_update_8003_field_create_instance($field, $instance);
}
/**
* Migrate forum containers from variable to field values.
*/
function forum_update_8002(&$sandbox) {
// Initialize total values to process.
if (!isset($sandbox['total'])) {
$containers = update_variable_get('forum_containers', array());
$vocabulary = config('forum.settings')->get('vocabulary');
$sandbox['containers'] = $containers;
$sandbox['vocabulary'] = $vocabulary;
$sandbox['total'] = count($containers);
$sandbox['processed'] = 0;
}
if ($sandbox['total']) {
// Retrieve next 20 containers to migrate.
$containers = array_splice($containers, $sandbox['processed'], 20);
foreach ($containers as $tid) {
// Add a row to the field data and revision tables.
db_insert('taxonomy_term__forum_container')
->fields(array(
'bundle' => $sandbox['vocabulary'],
'entity_id' => $tid,
'revision_id' => $tid,
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'delta' => 0,
'forum_container_value' => 1,
))
->execute();
db_insert('taxonomy_term_revision__forum_container')
->fields(array(
'bundle' => $vocabulary,
'entity_id' => $tid,
'revision_id' => $tid,
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'delta' => 0,
'forum_container_value' => 1,
))
->execute();
}
// Report status.
$sandbox['processed'] += count($containers);
}
$sandbox['#finished'] = $sandbox['total'] ? $sandbox['processed'] / $sandbox['total'] : 1;
}
/**
* Remove the forum_containers variable.
*/
function forum_update_8003() {
update_variable_del('forum_containers');
}
This diff is collapsed.
<?php
/**
* @file
* User page callbacks for the Forum module.
*/
/**
* Page callback: Prints a forum listing.
*
* @param $forum_term
* A tree of all forums for a given taxonomy term ID. Defaults to NULL. See
* the return object of forum_forum_load() for a complete definition.
*
* @return
* A string containing HTML representing the themed forum listing.
*
* @see forum_menu()
*/
function forum_page($forum_term = NULL) {
$config = Drupal::config('forum.settings');
$vocabulary = entity_load('taxonomy_vocabulary', $config->get('vocabulary'));
if (!isset($forum_term)) {
// On the main page, display all the top-level forums.
$forum_term = forum_forum_load(0);
// Set the page title to forum's vocabulary name.
drupal_set_title($vocabulary->label());
}
if ($forum_term->id() && array_search($forum_term->id(), $config->get('containers')) === FALSE) {
// Add RSS feed for forums.
drupal_add_feed('taxonomy/term/' . $forum_term->id() . '/feed', 'RSS - ' . $forum_term->label());
}
if (empty($forum_term->forums) && empty($forum_term->parents)) {
// Root of empty forum.
drupal_set_title(t('No forums defined'));
}
$forum_per_page = $config->get('topics.page_limit');
$sort_by = $config->get('topics.order');
if (empty($forum_term->container)) {
$topics = forum_get_topics($forum_term->id(), $sort_by, $forum_per_page);
}
else {
$topics = '';
}
$build = array(
'#theme' => 'forums',
'#forums' => $forum_term->forums,
'#topics' => $topics,
'#parents' => $forum_term->parents,
'#tid' => $forum_term->id(),
'#sortby' => $sort_by,
'#forums_per_page' => $forum_per_page,
);
$build['#attached']['css'][] = drupal_get_path('module', 'forum') . '/css/forum.module.css';
return $build;
}
......@@ -4,12 +4,28 @@ forum_delete:
_form: 'Drupal\forum\Form\DeleteForm'
requirements:
_permission: 'administer forums'
forum_settings:
pattern: '/admin/structure/forum/settings'
defaults:
_form: '\Drupal\forum\ForumSettingsForm'
requirements:
_permission: 'administer forums'
forum_index:
pattern: '/forum'
defaults:
_content: 'Drupal\forum\Controller\ForumController::forumIndex'
requirements:
_permission: 'access content'
forum_page:
pattern: '/forum/{taxonomy_term}'
defaults:
_content: 'Drupal\forum\Controller\ForumController::forumPage'
requirements:
_permission: 'access content'
forum_add_container:
pattern: 'admin/structure/forum/add/container'
defaults:
......
services:
forum_manager:
class: Drupal\forum\ForumManager
arguments: ['@config.factory', '@plugin.manager.entity', '@database', '@field.info', '@string_translation']
forum.breadcrumb:
class: Drupal\forum\ForumBreadcrumbBuilder
arguments: ['@entity.manager', '@config.factory']
arguments: ['@entity.manager', '@config.factory', '@forum_manager']
tags:
- { name: breadcrumb_builder, priority: 1001 }
......@@ -7,10 +7,15 @@
namespace Drupal\forum\Controller;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Config\Config;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\forum\ForumManagerInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\TermStorageControllerInterface;
use Drupal\taxonomy\VocabularyStorageControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -18,6 +23,13 @@
*/
class ForumController implements ContainerInjectionInterface {
/**
* Forum manager service.
*
* @var \Drupal\forum\ForumManagerInterface
*/
protected $forumManager;
/**
* Entity Manager Service.
*
......@@ -32,38 +44,142 @@ class ForumController implements ContainerInjectionInterface {
*/
protected $config;
/**
* Vocabulary storage controller.
*
* @var \Drupal\taxonomy\VocabularyStorageControllerInterface
*/
protected $vocabularyStorageController;
/**
* Term storage controller.
*
* @var \Drupal\taxonomy\TermStorageControllerInterface
*/
protected $storageController;
protected $termStorageController;
/**
* Translation manager service.
*
* @var \Drupal\Core\StringTranslation\TranslationManager
*/
protected $translationManager;
/**
* Constructs a ForumController object.
*
* @param \Drupal\Core\Config\Config $config
* Config object for forum.settings.
* @param \Drupal\forum\ForumManagerInterface $forum_manager
* The forum manager service.
* @param \Drupal\taxonomy\VocabularyStorageControllerInterface $vocabulary_storage_controller
* Vocabulary storage controller.
* @param \Drupal\taxonomy\TermStorageControllerInterface $term_storage_controller
* Term storage controller.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager service.
* @param \Drupal\Core\StringTranslation\TranslationManager $translation_manager
* The translation manager service.
*/
public function __construct(Config $config, ForumManagerInterface $forum_manager, VocabularyStorageControllerInterface $vocabulary_storage_controller, TermStorageControllerInterface $term_storage_controller, EntityManager $entity_manager, TranslationManager $translation_manager) {
$this->config = $config;
$this->forumManager = $forum_manager;
$this->vocabularyStorageController = $vocabulary_storage_controller;
$this->termStorageController = $term_storage_controller;
$this->entityManager = $entity_manager;
$this->translationManager = $translation_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory')->get('forum.settings'),
$container->get('forum_manager'),
$container->get('entity.manager')->getStorageController('taxonomy_vocabulary'),
$container->get('entity.manager')->getStorageController('taxonomy_term'),
$container->get('entity.manager'),
$container->get('config.factory'),
$container->get('entity.manager')->getStorageController('taxonomy_term')
$container->get('string_translation')
);
}
/**
* Constructs a ForumController object.
* Returns forum page for a given forum.
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager service.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The factory for configuration objects.
* @param \Drupal\taxonomy\TermStorageControllerInterface $storage_controller
* The term storage controller.
* @param \Drupal\taxonomy\TermInterface $taxonomy_term
* The forum to render the page for.
*
* @return array
* A render array.
*/
public function __construct(EntityManager $entity_manager, ConfigFactory $config_factory, TermStorageControllerInterface $storage_controller) {
$this->entityManager = $entity_manager;
$this->config = $config_factory->get('forum.settings');
$this->storageController = $storage_controller;
public function forumPage(TermInterface $taxonomy_term) {
// Get forum details.
$taxonomy_term->forums = $this->forumManager->getChildren($this->config->get('vocabulary'), $taxonomy_term->id());
$taxonomy_term->parents = $this->forumManager->getParents($taxonomy_term->id());
if (empty($taxonomy_term->forum_container->value)) {
// Add RSS feed for forums.
drupal_add_feed('taxonomy/term/' . $taxonomy_term->id() . '/feed', 'RSS - ' . $taxonomy_term->label());
}
if (empty($taxonomy_term->forum_container->value)) {
$topics = $this->forumManager->getTopics($taxonomy_term->id());
}
else {
$topics = '';
}
return $this->build($taxonomy_term->forums, $taxonomy_term, $topics, $taxonomy_term->parents);
}
/**
* Returns forum index page.
*
* @return array
* A render array.
*/
public function forumIndex() {
$vocabulary = $this->vocabularyStorageController->load($this->config->get('vocabulary'));
$index = $this->forumManager->getIndex();
$build = $this->build($index->forums, $index);
if (empty($index->forums)) {
// Root of empty forum.
$build['#title'] = $this->translationManager->translate('No forums defined');
}
else {
// Set the page title to forum's vocabulary name.
$build['#title'] = $vocabulary->label();
}
return $build;
}
/**
* Returns a renderable forum index page array.
*
* @param array $forums
* A list of forums.
* @param \Drupal\taxonomy\TermInterface $term
* The taxonomy term of the forum.
* @param array $topics
* The topics of this forum.
* @param array $parents
* The parent forums in relation this forum.
*
* @return array
* A render array.
*/
protected function build($forums, TermInterface $term, $topics = array(), $parents = array()) {
$build = array(
'#theme' => 'forums',
'#forums' => $forums,
'#topics' => $topics,
'#parents' => $parents,
'#term' => $term,
'#sortby' => $this->config->get('topics.order'),
'#forums_per_page' => $this->config->get('topics.page_limit'),
);
// @todo Make this a library - see https://drupal.org/node/2028113.
$build['#attached']['css'][] = drupal_get_path('module', 'forum') . '/css/forum.module.css';
return $build;
}
/**
......@@ -74,8 +190,9 @@ public function __construct(EntityManager $entity_manager, ConfigFactory $config
*/
public function addForum() {
$vid = $this->config->get('vocabulary');
$taxonomy_term = $this->storageController->create(array(
$taxonomy_term = $this->termStorageController->create(array(
'vid' => $vid,
'forum_controller' => 0,
));
return $this->entityManager->getForm($taxonomy_term, 'forum');
}
......@@ -88,8 +205,9 @@ public function addForum() {
*/
public function addContainer() {
$vid = $this->config->get('vocabulary');
$taxonomy_term = $this->storageController->create(array(
$taxonomy_term = $this->termStorageController->create(array(
'vid' => $vid,
'forum_container' => 1,
));
return $this->entityManager->getForm($taxonomy_term, 'container');
}
......
......@@ -42,14 +42,8 @@ public function form(array $form, array &$form_state) {
*/
public function save(array $form, array &$form_state) {
$is_new = $this->entity->isNew();
$this->entity->forum_container = TRUE;
$term = parent::save($form, $form_state);
if ($is_new) {
// Update config item to track the container terms.
$config = $this->configFactory->get('forum.settings');
$containers = $config->get('containers');
$containers[] = $term->id();
$config->set('containers', $containers)->save();
}
}
}
......@@ -10,6 +10,8 @@
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityManager;
use Drupal\forum\ForumManagerInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
/**
* Class to define the forum breadcrumb builder.
......@@ -30,6 +32,13 @@ class ForumBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/
protected $entityManager;
/**
* The forum manager service.
*
* @var \Drupal\forum\ForumManagerInterface
*/
protected $forumManager;
/**
* Constructs a new ForumBreadcrumbBuilder.
*
......@@ -37,41 +46,34 @@ class ForumBreadcrumbBuilder implements BreadcrumbBuilderInterface {
* The entity manager.
* @param \Drupal\Core\Config\ConfigFactory $configFactory
* The configuration factory.
* @param \Drupal\forum\ForumManagerInterface $forum_manager
* The forum manager service.
*/
public function __construct(EntityManager $entity_manager, ConfigFactory $configFactory) {
public function __construct(EntityManager $entity_manager, ConfigFactory $configFactory, ForumManagerInterface $forum_manager) {
$this->entityManager = $entity_manager;
$this->config = $configFactory->get('forum.settings');
$this->forumManager = $forum_manager;
}
/**
* {@inheritdoc}
*/
public function build(array $attributes) {
// @todo This only works for legacy routes. Once node/% and forum/% are
// converted to the new router this code will need to be updated.
if (isset($attributes['_drupal_menu_item'])) {
$item = $attributes['_drupal_menu_item'];
switch ($item['path']) {
case 'node/%':
$node = $item['map'][1];
// Load the object in case of missing wildcard loaders.
$node = is_object($node) ? $node : node_load($node);
if (_forum_node_check_node_type($node)) {
$breadcrumb = $this->forumPostBreadcrumb($node);
}
break;
case 'forum/%':
$term = $item['map'][1];
// Load the object in case of missing wildcard loaders.
$term = is_object($term) ? $term : forum_forum_load($term);
$breadcrumb = $this->forumTermBreadcrumb($term);
break;
if (isset($attributes['_drupal_menu_item']) && ($item = $attributes['_drupal_menu_item']) && $item['path'] == 'node/%') {
$node = $item['map'][1];
// Load the object in case of missing wildcard loaders.
$node = is_object($node) ? $node : node_load($node);
if ($this->forumManager->checkNodeType($node)) {
$breadcrumb = $this->forumPostBreadcrumb($node);
}
}
if (!empty($attributes[RouteObjectInterface::ROUTE_NAME]) && $attributes[RouteObjectInterface::ROUTE_NAME] == 'forum_page' && isset($attributes['taxonomy_term'])) {
$breadcrumb = $this->forumTermBreadcrumb($attributes['taxonomy_term']);
}
if (!empty($breadcrumb)) {
return $breadcrumb;
}
......
This diff is collapsed.
<?php
/**
* @file
* Contains \Drupal\forum\ForumManagerInterface.
*/
namespace Drupal\forum;
use Drupal\node\NodeInterface;
/**