You are here

class SocialCommentAdminOverview in Open Social 8.6

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview
  2. 8.3 modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview
  3. 8.4 modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview
  4. 8.5 modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview
  5. 8.7 modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview
  6. 8.8 modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview
  7. 10.3.x modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview
  8. 10.0.x modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview
  9. 10.1.x modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview
  10. 10.2.x modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php \Drupal\social_comment\Form\SocialCommentAdminOverview

Provides the comments overview administration form.

@internal

Hierarchy

Expanded class hierarchy of SocialCommentAdminOverview

File

modules/social_features/social_comment/src/Form/SocialCommentAdminOverview.php, line 20

Namespace

Drupal\social_comment\Form
View source
class SocialCommentAdminOverview extends FormBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The comment storage.
   *
   * @var \Drupal\comment\CommentStorageInterface
   */
  protected $commentStorage;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The tempstore factory.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\Renderer
   */
  protected $renderer;

  /**
   * Creates a CommentAdminOverview form.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager service.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The tempstore factory.
   * @param \Drupal\Core\Render\Renderer $renderer
   *   The renderer.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler, PrivateTempStoreFactory $temp_store_factory, Renderer $renderer) {
    $this->entityTypeManager = $entity_type_manager;
    $this->commentStorage = $entity_type_manager
      ->getStorage('comment');
    $this->dateFormatter = $date_formatter;
    $this->moduleHandler = $module_handler;
    $this->tempStoreFactory = $temp_store_factory;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('date.formatter'), $container
      ->get('module_handler'), $container
      ->get('tempstore.private'), $container
      ->get('renderer'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'comment_admin_overview';
  }

  /**
   * Form constructor for the comment overview administration form.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param string $type
   *   The type of the overview form ('approval' or 'new').
   *
   * @return array
   *   The form structure.
   */
  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 = [
      'author' => [
        'data' => $this
          ->t('Author'),
        'specifier' => 'name',
        'class' => [
          RESPONSIVE_PRIORITY_MEDIUM,
        ],
      ],
      'comment' => [
        'data' => $this
          ->t('Comment'),
        'specifier' => 'comment_body',
        '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()
      ->condition('status', $status)
      ->tableSort($header)
      ->pager(50)
      ->execute();

    /* @var $comments \Drupal\comment\CommentInterface[] */
    $comments = $this->commentStorage
      ->loadMultiple($cids);

    // Build a table listing the appropriate comments.
    $options = [];
    $destination = $this
      ->getDestinationArray();
    foreach ($comments as $comment) {

      // Get a render array for the comment body field. We'll render it in the
      // table.
      $comment_body = $comment->field_comment_body
        ->view('full');
      $options[$comment
        ->id()] = [
        'title' => [
          'data' => [
            '#title' => $comment
              ->getSubject() ?: $comment
              ->id(),
          ],
        ],
        'author' => [
          'data' => [
            '#theme' => 'username',
            '#account' => $comment
              ->getOwner(),
          ],
        ],
        'comment' => [
          'data' => [
            '#markup' => $this->renderer
              ->renderRoot($comment_body),
          ],
        ],
        'changed' => $this->dateFormatter
          ->format($comment
          ->getChangedTimeAcrossTranslations(), 'short'),
      ];

      // Create a list of operations.
      $comment_uri_options = $comment
        ->toUrl()
        ->getOptions() + [
        'query' => $destination,
      ];
      $links['view'] = [
        'title' => $this
          ->t('View'),
        'url' => $comment
          ->toUrl(),
      ];
      $links['edit'] = [
        'title' => $this
          ->t('Edit'),
        'url' => $comment
          ->toUrl('edit-form', $comment_uri_options),
      ];
      $links['delete'] = [
        'title' => $this
          ->t('Delete'),
        'url' => $comment
          ->toUrl('delete-form', $comment_uri_options),
      ];

      // Add a 'Translate' operations link if the comment is translatable.
      if ($this->moduleHandler
        ->moduleExists('content_translation') && $comment
        ->getCommentedEntity() instanceof ContentEntityInterface && $this->moduleHandler
        ->invoke('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;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setValue('comments', array_diff($form_state
      ->getValue('comments'), [
      0,
    ]));

    // We can't execute any 'Update options' if no comments were selected.
    if (count($form_state
      ->getValue('comments')) == 0) {
      $form_state
        ->setErrorByName('', $this
        ->t('Select one or more comments to perform the update on.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $operation = $form_state
      ->getValue('operation');
    $cids = $form_state
      ->getValue('comments');

    /** @var \Drupal\comment\CommentInterface[] $comments */
    $comments = $this->commentStorage
      ->loadMultiple($cids);
    if ($operation != 'delete') {
      foreach ($comments as $comment) {
        if ($operation == 'unpublish') {
          $comment
            ->setUnpublished();
        }
        elseif ($operation == 'publish') {
          $comment
            ->setPublished();
        }
        $comment
          ->save();
      }
      drupal_set_message($this
        ->t('The update has been performed.'));
      $form_state
        ->setRedirect('comment.admin');
    }
    else {
      $info = [];

      /** @var \Drupal\comment\CommentInterface $comment */
      foreach ($comments as $comment) {
        $langcode = $comment
          ->language()
          ->getId();
        $info[$comment
          ->id()][$langcode] = $langcode;
      }
      $this->tempStoreFactory
        ->get('comment_multiple_delete_confirm')
        ->set($this
        ->currentUser()
        ->id(), $info);
      $form_state
        ->setRedirect('comment.multiple_delete_confirm');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SocialCommentAdminOverview::$commentStorage protected property The comment storage.
SocialCommentAdminOverview::$dateFormatter protected property The date formatter service.
SocialCommentAdminOverview::$entityTypeManager protected property The entity type manager.
SocialCommentAdminOverview::$moduleHandler protected property The module handler.
SocialCommentAdminOverview::$renderer protected property The renderer.
SocialCommentAdminOverview::$tempStoreFactory protected property The tempstore factory.
SocialCommentAdminOverview::buildForm public function Form constructor for the comment overview administration form. Overrides FormInterface::buildForm
SocialCommentAdminOverview::create public static function Instantiates a new instance of this class. Overrides FormBase::create
SocialCommentAdminOverview::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SocialCommentAdminOverview::submitForm public function Form submission handler. Overrides FormInterface::submitForm
SocialCommentAdminOverview::validateForm public function Form validation handler. Overrides FormBase::validateForm
SocialCommentAdminOverview::__construct public function Creates a CommentAdminOverview form.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.