Commit cfe3e159 authored by alexpott's avatar alexpott
Browse files

Revert "Issue #2289989 by marcingy, andypost: Remove comment_add and inline final occurrence."

This reverts commit 4b91f16d.
parent 4b91f16d
......@@ -10,17 +10,14 @@
/**
* Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
*
* Using this trait will add t() and formatPlural() methods to the class. These
* must be used for every translatable string, similar to how procedural code
* must use the global functions t() and formatPlural(). This allows string
* extractor tools to find translatable strings.
* Injected translation can be performed by using a protected method ::t(), so
* string extractor tools can find all translatable strings. This method must
* wrap \Drupal\Core\StringTranslation\TranslationInterface::translate().
* This trait provides this method in a re-usable way.
*
* If the class is capable of injecting services from the container, it should
* inject the 'string_translation' service and assign it to
* $this->stringTranslation.
*
* @see \Drupal\Core\StringTranslation\TranslationInterface
* @see container
* Procedural code must use the global function t(). Any other approach will
* result in untranslatable strings, because the string extractor will not be
* able to find them.
*
* @ingroup i18n
*/
......
......@@ -505,6 +505,33 @@ function comment_node_view_alter(array &$build, EntityInterface $node, EntityVie
}
}
/**
* Returns a rendered form to comment the given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to which the comments are in reply to.
* @param string $field_name
* The field name where the comments were entered.
* @param int $pid
* (optional) Some comments are replies to other comments. In those cases,
* $pid is the parent comment's comment ID. Defaults to NULL.
*
* @return array
* The renderable array for the comment addition form.
*/
function comment_add(EntityInterface $entity, $field_name = 'comment', $pid = NULL) {
$field = Fieldconfig::loadByName($entity->getEntityTypeId(), $field_name);
$values = array(
'entity_type' => $entity->getEntityTypeId(),
'entity_id' => $entity->id(),
'field_name' => $field_name,
'comment_type' => $field->getSetting('bundle'),
'pid' => $pid,
);
$comment = entity_create('comment', $values);
return \Drupal::service('entity.form_builder')->getForm($comment);
}
/**
* Retrieves comments for a thread.
*
......
......@@ -10,7 +10,6 @@
use Drupal\comment\CommentStorageInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Entity\EntityViewBuilderInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
......@@ -65,13 +64,6 @@ public static function defaultSettings() {
*/
protected $viewBuilder;
/**
* The entity form builder.
*
* @var \Drupal\Core\Entity\EntityFormBuilderInterface
*/
protected $entityFormBuilder;
/**
* {@inheritdoc}
*/
......@@ -86,8 +78,7 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration['third_party_settings'],
$container->get('current_user'),
$container->get('entity.manager')->getStorage('comment'),
$container->get('entity.manager')->getViewBuilder('comment'),
$container->get('entity.form_builder')
$container->get('entity.manager')->getViewBuilder('comment')
);
}
......@@ -114,15 +105,12 @@ public static function create(ContainerInterface $container, array $configuratio
* The comment storage.
* @param \Drupal\Core\Entity\EntityViewBuilderInterface $comment_view_builder
* The comment view builder.
* @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
* The entity form builder.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, CommentStorageInterface $comment_storage, EntityViewBuilderInterface $comment_view_builder, EntityFormBuilderInterface $entity_form_builder) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, CommentStorageInterface $comment_storage, EntityViewBuilderInterface $comment_view_builder) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->viewBuilder = $comment_view_builder;
$this->storage = $comment_storage;
$this->currentUser = $current_user;
$this->entityFormBuilder = $entity_form_builder;
}
/**
......@@ -186,14 +174,7 @@ public function viewElements(FieldItemListInterface $items) {
// All users in the "anonymous" role can use the same form: it is fine
// for this form to be stored in the render cache.
if ($this->currentUser->isAnonymous()) {
$comment = $this->storage->create(array(
'entity_type' => $entity->getEntityTypeId(),
'entity_id' => $entity->id(),
'field_name' => $field_name,
'comment_type' => $this->getFieldSetting('comment_type'),
'pid' => NULL,
));
$output['comment_form'] = $this->entityFormBuilder->getForm($comment);
$output['comment_form'] = comment_add($entity, $field_name);
}
// All other users need a user-specific form, which would break the
// render cache: hence use a #post_render_cache callback.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment