CommentTypeForm.php 5.38 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Drupal\comment;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\language\Entity\ContentLanguageSettings;
10
use Psr\Log\LoggerInterface;
11 12 13
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
14
 * Base form handler for comment type edit forms.
15 16 17 18 19 20 21 22 23 24 25
 */
class CommentTypeForm extends EntityForm {

  /**
   * Entity manager service.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
26
   * A logger instance.
27
   *
28
   * @var \Psr\Log\LoggerInterface
29 30 31
   */
  protected $logger;

32 33 34 35 36 37 38
  /**
   * The comment manager.
   *
   * @var \Drupal\comment\CommentManagerInterface
   */
  protected $commentManager;

39 40 41 42 43 44
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager'),
45 46
      $container->get('logger.factory')->get('comment'),
      $container->get('comment.manager')
47 48 49 50 51 52 53 54
    );
  }

  /**
   * Constructs a CommentTypeFormController
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
55 56
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
57 58
   * @param \Drupal\comment\CommentManagerInterface $comment_manager
   *   The comment manager.
59
   */
60
  public function __construct(EntityManagerInterface $entity_manager, LoggerInterface $logger, CommentManagerInterface $comment_manager) {
61 62
    $this->entityManager = $entity_manager;
    $this->logger = $logger;
63
    $this->commentManager = $comment_manager;
64 65 66 67 68
  }

  /**
   * {@inheritdoc}
   */
69
  public function form(array $form, FormStateInterface $form_state) {
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    $form = parent::form($form, $form_state);

    $comment_type = $this->entity;

    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label'),
      '#maxlength' => 255,
      '#default_value' => $comment_type->label(),
      '#required' => TRUE,
    );
    $form['id'] = array(
      '#type' => 'machine_name',
      '#default_value' => $comment_type->id(),
      '#machine_name' => array(
        'exists' => '\Drupal\comment\Entity\CommentType::load',
      ),
      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
      '#disabled' => !$comment_type->isNew(),
    );

    $form['description'] = array(
      '#type' => 'textarea',
93
      '#default_value' => $comment_type->getDescription(),
94
      '#description' => t('Describe this comment type. The text will be displayed on the <em>Comment types</em> administration overview page.'),
95 96 97
      '#title' => t('Description'),
    );

98 99 100
    if ($comment_type->isNew()) {
      $options = array();
      foreach ($this->entityManager->getDefinitions() as $entity_type) {
101 102 103
        // Only expose entities that have field UI enabled, only those can
        // get comment fields added in the UI.
        if ($entity_type->get('field_ui_base_route')) {
104 105
          $options[$entity_type->id()] = $entity_type->getLabel();
        }
106
      }
107 108 109 110 111 112 113 114 115 116 117 118 119 120
      $form['target_entity_type_id'] = array(
        '#type' => 'select',
        '#default_value' => $comment_type->getTargetEntityTypeId(),
        '#title' => t('Target entity type'),
        '#options' => $options,
        '#description' => t('The target entity type can not be changed after the comment type has been created.')
      );
    }
    else {
      $form['target_entity_type_id_display'] = array(
        '#type' => 'item',
        '#markup' => $this->entityManager->getDefinition($comment_type->getTargetEntityTypeId())->getLabel(),
        '#title' => t('Target entity type'),
      );
121 122 123 124 125 126 127 128 129
    }

    if ($this->moduleHandler->moduleExists('content_translation')) {
      $form['language'] = array(
        '#type' => 'details',
        '#title' => t('Language settings'),
        '#group' => 'additional_settings',
      );

130
      $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', $comment_type->id());
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      $form['language']['language_configuration'] = array(
        '#type' => 'language_configuration',
        '#entity_information' => array(
          'entity_type' => 'comment',
          'bundle' => $comment_type->id(),
        ),
        '#default_value' => $language_configuration,
      );

      $form['#submit'][] = 'language_configuration_element_submit';
    }

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
155
  public function save(array $form, FormStateInterface $form_state) {
156 157 158
    $comment_type = $this->entity;
    $status = $comment_type->save();

159
    $edit_link = $this->entity->link($this->t('Edit'));
160 161 162 163 164
    if ($status == SAVED_UPDATED) {
      drupal_set_message(t('Comment type %label has been updated.', array('%label' => $comment_type->label())));
      $this->logger->notice('Comment type %label has been updated.', array('%label' => $comment_type->label(), 'link' => $edit_link));
    }
    else {
165
      $this->commentManager->addBodyField($comment_type->id());
166
      drupal_set_message(t('Comment type %label has been added.', array('%label' => $comment_type->label())));
167
      $this->logger->notice('Comment type %label has been added.', array('%label' => $comment_type->label(), 'link' => $edit_link));
168 169
    }

170
    $form_state->setRedirectUrl($comment_type->urlInfo('collection'));
171 172 173
  }

}