function CommentAdminOverview::buildForm

Same name in other branches
  1. 9 core/modules/comment/src/Form/CommentAdminOverview.php \Drupal\comment\Form\CommentAdminOverview::buildForm()
  2. 8.9.x core/modules/comment/src/Form/CommentAdminOverview.php \Drupal\comment\Form\CommentAdminOverview::buildForm()
  3. 10 core/modules/comment/src/Form/CommentAdminOverview.php \Drupal\comment\Form\CommentAdminOverview::buildForm()

Form constructor for the comment overview administration form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

string $type: The type of the overview form ('approval' or 'new').

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/comment/src/Form/CommentAdminOverview.php, line 109

Class

CommentAdminOverview
Provides the comments overview administration form.

Namespace

Drupal\comment\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') {
    // Build an 'Update options' form.
    $form['options'] = [
        '#type' => 'details',
        '#title' => $this->t('Update options'),
        '#open' => TRUE,
        '#attributes' => [
            'class' => [
                'container-inline',
            ],
        ],
    ];
    if ($type == 'approval') {
        $options['publish'] = $this->t('Publish the selected comments');
    }
    else {
        $options['unpublish'] = $this->t('Unpublish the selected comments');
    }
    $options['delete'] = $this->t('Delete the selected comments');
    $form['options']['operation'] = [
        '#type' => 'select',
        '#title' => $this->t('Action'),
        '#title_display' => 'invisible',
        '#options' => $options,
        '#default_value' => 'publish',
    ];
    $form['options']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Update'),
    ];
    // Load the comments that need to be displayed.
    $status = $type == 'approval' ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED;
    $header = [
        'subject' => [
            'data' => $this->t('Subject'),
            'specifier' => 'subject',
        ],
        'author' => [
            'data' => $this->t('Author'),
            'specifier' => 'name',
            'class' => [
                RESPONSIVE_PRIORITY_MEDIUM,
            ],
        ],
        'posted_in' => [
            'data' => $this->t('Posted in'),
            'class' => [
                RESPONSIVE_PRIORITY_LOW,
            ],
        ],
        'changed' => [
            'data' => $this->t('Updated'),
            'specifier' => 'changed',
            'sort' => 'desc',
            'class' => [
                RESPONSIVE_PRIORITY_LOW,
            ],
        ],
        'operations' => $this->t('Operations'),
    ];
    $cids = $this->commentStorage
        ->getQuery()
        ->accessCheck(TRUE)
        ->condition('status', $status)
        ->tableSort($header)
        ->pager(50)
        ->execute();
    
    /** @var \Drupal\comment\CommentInterface[] $comments */
    $comments = $this->commentStorage
        ->loadMultiple($cids);
    // Build a table listing the appropriate comments.
    $options = [];
    $destination = $this->getDestinationArray();
    $commented_entity_ids = [];
    $commented_entities = [];
    foreach ($comments as $comment) {
        $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
    }
    foreach ($commented_entity_ids as $entity_type => $ids) {
        $commented_entities[$entity_type] = $this->entityTypeManager
            ->getStorage($entity_type)
            ->loadMultiple($ids);
    }
    foreach ($comments as $comment) {
        
        /** @var \Drupal\Core\Entity\EntityInterface $commented_entity */
        $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
        $comment_permalink = $comment->permalink();
        if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
            $attributes = $comment_permalink->getOption('attributes') ?: [];
            $attributes += [
                'title' => Unicode::truncate($body, 128),
            ];
            $comment_permalink->setOption('attributes', $attributes);
        }
        $options[$comment->id()] = [
            'title' => [
                'data' => [
                    '#title' => $comment->getSubject() ?: $comment->id(),
                ],
            ],
            'subject' => [
                'data' => [
                    '#type' => 'link',
                    '#title' => $comment->getSubject(),
                    '#url' => $comment_permalink,
                ],
            ],
            'author' => [
                'data' => [
                    '#theme' => 'username',
                    '#account' => $comment->getOwner(),
                ],
            ],
            'posted_in' => [
                'data' => [
                    '#type' => 'link',
                    '#title' => $commented_entity->label(),
                    '#access' => $commented_entity->access('view'),
                    '#url' => $commented_entity->toUrl(),
                ],
            ],
            'changed' => $this->dateFormatter
                ->format($comment->getChangedTimeAcrossTranslations(), 'short'),
        ];
        $comment_uri_options = $comment->toUrl()
            ->getOptions() + [
            'query' => $destination,
        ];
        $links = [];
        $links['edit'] = [
            'title' => $this->t('Edit'),
            'url' => $comment->toUrl('edit-form', $comment_uri_options),
        ];
        if ($this->moduleHandler
            ->moduleExists('content_translation') && content_translation_translate_access($comment)->isAllowed()) {
            $links['translate'] = [
                'title' => $this->t('Translate'),
                'url' => $comment->toUrl('drupal:content-translation-overview', $comment_uri_options),
            ];
        }
        $options[$comment->id()]['operations']['data'] = [
            '#type' => 'operations',
            '#links' => $links,
        ];
    }
    $form['comments'] = [
        '#type' => 'tableselect',
        '#header' => $header,
        '#options' => $options,
        '#empty' => $this->t('No comments available.'),
    ];
    $form['pager'] = [
        '#type' => 'pager',
    ];
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.