27335d61d9e2de85c463cdcf920bb6e59c83896a
[yaffs-website] / 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 Drupal\taxonomy\VocabularyInterface;
12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14 /**
15  * Provides forum overview form for the forum vocabulary.
16  *
17  * @internal
18  */
19 class Overview extends OverviewTerms {
20
21   /**
22    * Entity manager Service Object.
23    *
24    * @var \Drupal\Core\Entity\EntityManagerInterface
25    */
26   protected $entityManager;
27
28   /**
29    * Constructs a \Drupal\forum\Form\OverviewForm object.
30    *
31    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
32    *   The module handler service.
33    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
34    *   The entity manager service.
35    */
36   public function __construct(ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager) {
37     parent::__construct($module_handler, $entity_manager);
38     $this->entityManager = $entity_manager;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getFormId() {
45     return 'forum_overview';
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
52     $forum_config = $this->config('forum.settings');
53     $vid = $forum_config->get('vocabulary');
54     $vocabulary = $this->entityManager->getStorage('taxonomy_vocabulary')->load($vid);
55     if (!$vocabulary) {
56       throw new NotFoundHttpException();
57     }
58
59     // Build base taxonomy term overview.
60     $form = parent::buildForm($form, $form_state, $vocabulary);
61
62     foreach (Element::children($form['terms']) as $key) {
63       if (isset($form['terms'][$key]['#term'])) {
64         /** @var \Drupal\taxonomy\TermInterface $term */
65         $term = $form['terms'][$key]['#term'];
66         $form['terms'][$key]['term']['#url'] = Url::fromRoute('forum.page', ['taxonomy_term' => $term->id()]);
67
68         if (!empty($term->forum_container->value)) {
69           $title = $this->t('edit container');
70           $url = Url::fromRoute('entity.taxonomy_term.forum_edit_container_form', ['taxonomy_term' => $term->id()]);
71         }
72         else {
73           $title = $this->t('edit forum');
74           $url = Url::fromRoute('entity.taxonomy_term.forum_edit_form', ['taxonomy_term' => $term->id()]);
75         }
76
77         // Re-create the operations column and add only the edit link.
78         $form['terms'][$key]['operations'] = [
79           '#type' => 'operations',
80           '#links' => [
81             'edit' => [
82               'title' => $title,
83               'url' => $url,
84             ],
85           ],
86         ];
87
88       }
89     }
90
91     // Remove the alphabetical reset.
92     unset($form['actions']['reset_alphabetical']);
93
94     // Use the existing taxonomy overview submit handler.
95     $form['terms']['#empty'] = $this->t('No containers or forums available. <a href=":container">Add container</a> or <a href=":forum">Add forum</a>.', [
96       ':container' => $this->url('forum.add_container'),
97       ':forum' => $this->url('forum.add_forum'),
98     ]);
99     return $form;
100   }
101
102 }