Newer
Older
Angie Byron
committed
<?php
namespace Drupal\forum\Form;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
Angie Byron
committed
use Drupal\taxonomy\Form\OverviewTerms;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\taxonomy\VocabularyInterface;
Angie Byron
committed
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Angie Byron
committed
* Provides forum overview form for the forum vocabulary.
*
* @internal
Angie Byron
committed
*/
class Overview extends OverviewTerms {
/**
* Entity manager Service Object.
*
Angie Byron
committed
* @var \Drupal\Core\Entity\EntityManagerInterface
Angie Byron
committed
*/
protected $entityManager;
/**
* Constructs a \Drupal\forum\Form\OverviewForm object.
*
Alex Pott
committed
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Angie Byron
committed
* The entity manager service.
*/
Alex Pott
committed
public function __construct(ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager) {
parent::__construct($module_handler, $entity_manager);
Angie Byron
committed
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getFormId() {
Angie Byron
committed
return 'forum_overview';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
Angie Byron
committed
$forum_config = $this->config('forum.settings');
Angie Byron
committed
$vid = $forum_config->get('vocabulary');
catch
committed
$vocabulary = $this->entityManager->getStorage('taxonomy_vocabulary')->load($vid);
Angie Byron
committed
if (!$vocabulary) {
throw new NotFoundHttpException();
}
// Build base taxonomy term overview.
$form = parent::buildForm($form, $form_state, $vocabulary);
Angie Byron
committed
foreach (Element::children($form['terms']) as $key) {
Angie Byron
committed
if (isset($form['terms'][$key]['#term'])) {
/** @var \Drupal\taxonomy\TermInterface $term */
Angie Byron
committed
$term = $form['terms'][$key]['#term'];
$form['terms'][$key]['term']['#url'] = Url::fromRoute('forum.page', ['taxonomy_term' => $term->id()]);
Angie Byron
committed
if (!empty($term->forum_container->value)) {
$title = $this->t('edit container');
$url = Url::fromRoute('entity.taxonomy_term.forum_edit_container_form', ['taxonomy_term' => $term->id()]);
Angie Byron
committed
}
else {
$title = $this->t('edit forum');
$url = Url::fromRoute('entity.taxonomy_term.forum_edit_form', ['taxonomy_term' => $term->id()]);
Angie Byron
committed
}
// Re-create the operations column and add only the edit link.
$form['terms'][$key]['operations'] = [
'#type' => 'operations',
'#links' => [
'edit' => [
'title' => $title,
'url' => $url,
],
],
];
Angie Byron
committed
}
}
// Remove the alphabetical reset.
unset($form['actions']['reset_alphabetical']);
// Use the existing taxonomy overview submit handler.
$form['terms']['#empty'] = $this->t('No containers or forums available. <a href=":container">Add container</a> or <a href=":forum">Add forum</a>.', [
':container' => $this->url('forum.add_container'),
':forum' => $this->url('forum.add_forum')
]);
Angie Byron
committed
return $form;
}
}