Version 1
[yaffs-website] / web / core / modules / forum / src / Form / Overview.php
1 <?php
2
3 namespace Drupal\forum\Form;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
8 use Drupal\Core\Url;
9 use Drupal\taxonomy\Form\OverviewTerms;
10 use Drupal\Core\Extension\ModuleHandlerInterface;
11 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13 /**
14  * Provides forum overview form for the forum vocabulary.
15  */
16 class Overview extends OverviewTerms {
17
18   /**
19    * Entity manager Service Object.
20    *
21    * @var \Drupal\Core\Entity\EntityManagerInterface
22    */
23   protected $entityManager;
24
25   /**
26    * Constructs a \Drupal\forum\Form\OverviewForm object.
27    *
28    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
29    *   The module handler service.
30    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
31    *   The entity manager service.
32    */
33   public function __construct(ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager) {
34     parent::__construct($module_handler, $entity_manager);
35     $this->entityManager = $entity_manager;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function getFormId() {
42     return 'forum_overview';
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function buildForm(array $form, FormStateInterface $form_state) {
49     $forum_config = $this->config('forum.settings');
50     $vid = $forum_config->get('vocabulary');
51     $vocabulary = $this->entityManager->getStorage('taxonomy_vocabulary')->load($vid);
52     if (!$vocabulary) {
53       throw new NotFoundHttpException();
54     }
55
56     // Build base taxonomy term overview.
57     $form = parent::buildForm($form, $form_state, $vocabulary);
58
59     foreach (Element::children($form['terms']) as $key) {
60       if (isset($form['terms'][$key]['#term'])) {
61         $term = $form['terms'][$key]['#term'];
62         $form['terms'][$key]['term']['#url'] = Url::fromRoute('forum.page', ['taxonomy_term' => $term->id()]);
63         unset($form['terms'][$key]['operations']['#links']['delete']);
64         $route_parameters = $form['terms'][$key]['operations']['#links']['edit']['url']->getRouteParameters();
65         if (!empty($term->forum_container->value)) {
66           $form['terms'][$key]['operations']['#links']['edit']['title'] = $this->t('edit container');
67           $form['terms'][$key]['operations']['#links']['edit']['url'] = Url::fromRoute('entity.taxonomy_term.forum_edit_container_form', $route_parameters);
68         }
69         else {
70           $form['terms'][$key]['operations']['#links']['edit']['title'] = $this->t('edit forum');
71           $form['terms'][$key]['operations']['#links']['edit']['url'] = Url::fromRoute('entity.taxonomy_term.forum_edit_form', $route_parameters);
72         }
73         // We don't want the redirect from the link so we can redirect the
74         // delete action.
75         unset($form['terms'][$key]['operations']['#links']['edit']['query']['destination']);
76       }
77     }
78
79     // Remove the alphabetical reset.
80     unset($form['actions']['reset_alphabetical']);
81
82     // Use the existing taxonomy overview submit handler.
83     $form['terms']['#empty'] = $this->t('No containers or forums available. <a href=":container">Add container</a> or <a href=":forum">Add forum</a>.', [
84       ':container' => $this->url('forum.add_container'),
85       ':forum' => $this->url('forum.add_forum')
86     ]);
87     return $form;
88   }
89
90 }