2008-11-25 02:37:33 +00:00
|
|
|
<?php
|
|
|
|
|
2012-09-12 09:18:04 +00:00
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
2013-11-29 09:40:59 +00:00
|
|
|
use Drupal\comment\CommentInterface;
|
2012-06-15 16:43:34 +00:00
|
|
|
|
2008-11-25 02:37:33 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Hooks provided by the Comment module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup hooks
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2009-08-17 13:10:45 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Act on a comment being inserted or updated.
|
2009-08-17 13:10:45 +00:00
|
|
|
*
|
2013-06-11 16:38:12 +00:00
|
|
|
* This hook is invoked from $comment->save() before the comment is saved to the
|
2012-01-03 04:36:15 +00:00
|
|
|
* database.
|
2009-08-17 13:10:45 +00:00
|
|
|
*
|
2013-10-03 11:26:25 +00:00
|
|
|
* @param \Drupal\comment\Comment $comment
|
2009-08-17 13:10:45 +00:00
|
|
|
* The comment object.
|
|
|
|
*/
|
2012-05-03 05:49:41 +00:00
|
|
|
function hook_comment_presave(Drupal\comment\Comment $comment) {
|
2009-08-17 13:10:45 +00:00
|
|
|
// Remove leading & trailing spaces from the comment subject.
|
2013-01-14 10:59:47 +00:00
|
|
|
$comment->subject->value = trim($comment->subject->value);
|
2009-08-17 13:10:45 +00:00
|
|
|
}
|
|
|
|
|
2008-11-25 02:37:33 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Respond to creation of a new comment.
|
2008-11-25 02:37:33 +00:00
|
|
|
*
|
2013-10-03 11:26:25 +00:00
|
|
|
* @param \Drupal\comment\Comment $comment
|
2009-07-01 20:39:20 +00:00
|
|
|
* The comment object.
|
2009-01-04 16:10:48 +00:00
|
|
|
*/
|
2012-05-03 05:49:41 +00:00
|
|
|
function hook_comment_insert(Drupal\comment\Comment $comment) {
|
2009-03-13 14:32:07 +00:00
|
|
|
// Reindex the node when comments are added.
|
2013-09-27 15:34:47 +00:00
|
|
|
if ($comment->entity_type->value == 'node') {
|
|
|
|
node_reindex_node_search($comment->entity_id->value);
|
|
|
|
}
|
2009-01-04 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Respond to updates to a comment.
|
2009-01-04 16:10:48 +00:00
|
|
|
*
|
2013-10-03 11:26:25 +00:00
|
|
|
* @param \Drupal\comment\Comment $comment
|
2009-07-01 20:39:20 +00:00
|
|
|
* The comment object.
|
2009-01-04 16:10:48 +00:00
|
|
|
*/
|
2012-05-03 05:49:41 +00:00
|
|
|
function hook_comment_update(Drupal\comment\Comment $comment) {
|
2009-03-13 14:32:07 +00:00
|
|
|
// Reindex the node when comments are updated.
|
2013-09-27 15:34:47 +00:00
|
|
|
if ($comment->entity_type->value == 'node') {
|
|
|
|
node_reindex_node_search($comment->entity_id->value);
|
|
|
|
}
|
2008-11-25 02:37:33 +00:00
|
|
|
}
|
|
|
|
|
2013-01-16 17:37:23 +00:00
|
|
|
/**
|
|
|
|
* Act on a newly created comment.
|
|
|
|
*
|
|
|
|
* This hook runs after a new comment object has just been instantiated. It can
|
|
|
|
* be used to set initial values, e.g. to provide defaults.
|
|
|
|
*
|
2013-08-18 21:16:19 +00:00
|
|
|
* @param \Drupal\comment\Entity\Comment $comment
|
2013-01-16 17:37:23 +00:00
|
|
|
* The comment object.
|
|
|
|
*/
|
2013-08-18 21:16:19 +00:00
|
|
|
function hook_comment_create(\Drupal\comment\Entity\Comment $comment) {
|
2013-01-16 17:37:23 +00:00
|
|
|
if (!isset($comment->foo)) {
|
|
|
|
$comment->foo = 'some_initial_value';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-10 05:50:08 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Act on comments being loaded from the database.
|
2009-07-10 05:50:08 +00:00
|
|
|
*
|
2012-04-13 08:01:13 +00:00
|
|
|
* @param array $comments
|
2009-07-10 05:50:08 +00:00
|
|
|
* An array of comment objects indexed by cid.
|
|
|
|
*/
|
2012-05-03 05:49:41 +00:00
|
|
|
function hook_comment_load(Drupal\comment\Comment $comments) {
|
2009-07-10 05:50:08 +00:00
|
|
|
$result = db_query('SELECT cid, foo FROM {mytable} WHERE cid IN (:cids)', array(':cids' => array_keys($comments)));
|
|
|
|
foreach ($result as $record) {
|
|
|
|
$comments[$record->cid]->foo = $record->foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 16:10:48 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Act on a comment that is being assembled before rendering.
|
2009-01-04 16:10:48 +00:00
|
|
|
*
|
2013-08-18 21:16:19 +00:00
|
|
|
* @param \Drupal\comment\Entity\Comment $comment $comment
|
2009-01-04 16:10:48 +00:00
|
|
|
* Passes in the comment the action is being performed on.
|
2013-12-12 23:34:44 +00:00
|
|
|
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
|
2013-01-08 19:16:16 +00:00
|
|
|
* The entity_display object holding the display options configured for the
|
|
|
|
* comment components.
|
2010-10-03 01:15:34 +00:00
|
|
|
* @param $view_mode
|
|
|
|
* View mode, e.g. 'full', 'teaser'...
|
|
|
|
* @param $langcode
|
|
|
|
* The language code used for rendering.
|
2010-10-23 15:30:34 +00:00
|
|
|
*
|
|
|
|
* @see hook_entity_view()
|
2009-01-04 16:10:48 +00:00
|
|
|
*/
|
2013-12-12 23:34:44 +00:00
|
|
|
function hook_comment_view(\Drupal\comment\Entity\Comment $comment, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode, $langcode) {
|
2013-01-08 19:16:16 +00:00
|
|
|
// Only do the extra work if the component is configured to be displayed.
|
|
|
|
// This assumes a 'mymodule_addition' extra field has been defined for the
|
|
|
|
// node type in hook_field_extra_fields().
|
|
|
|
if ($display->getComponent('mymodule_addition')) {
|
|
|
|
$comment->content['mymodule_addition'] = array(
|
|
|
|
'#markup' => mymodule_addition($comment),
|
|
|
|
'#theme' => 'mymodule_my_additional_field',
|
|
|
|
);
|
|
|
|
}
|
2009-01-04 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 13:35:21 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Alter the results of comment_view().
|
2009-11-07 13:35:21 +00:00
|
|
|
*
|
2012-01-03 04:36:15 +00:00
|
|
|
* This hook is called after the content has been assembled in a structured
|
|
|
|
* array and may be used for doing processing which requires that the complete
|
|
|
|
* comment content structure has been built.
|
2009-11-07 13:35:21 +00:00
|
|
|
*
|
2012-01-03 04:36:15 +00:00
|
|
|
* If the module wishes to act on the rendered HTML of the comment rather than
|
|
|
|
* the structured content array, it may use this hook to add a #post_render
|
2012-05-04 19:53:43 +00:00
|
|
|
* callback. Alternatively, it could also implement hook_preprocess_HOOK() for
|
Issue #1898054 by pixelmord, jenlampton, steveoliver, EVIIILJ, c4rl, vlad.dancer, Fabianx, joelpittet, jwilson3, thedavidmeister, shanethehat, Cottser: Convert comment module to Twig.
2013-05-24 17:04:14 +00:00
|
|
|
* comment.html.twig. See drupal_render() documentation for details.
|
2009-11-07 13:35:21 +00:00
|
|
|
*
|
|
|
|
* @param $build
|
|
|
|
* A renderable array representing the comment.
|
2013-08-18 21:16:19 +00:00
|
|
|
* @param \Drupal\comment\Entity\Comment $comment
|
2012-06-15 16:43:34 +00:00
|
|
|
* The comment being rendered.
|
2013-12-12 23:34:44 +00:00
|
|
|
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
|
2013-01-08 19:16:16 +00:00
|
|
|
* The entity_display object holding the display options configured for the
|
|
|
|
* comment components.
|
2009-11-07 13:35:21 +00:00
|
|
|
*
|
2009-12-21 13:47:32 +00:00
|
|
|
* @see comment_view()
|
2010-10-23 15:30:34 +00:00
|
|
|
* @see hook_entity_view_alter()
|
2009-11-07 13:35:21 +00:00
|
|
|
*/
|
2013-12-12 23:34:44 +00:00
|
|
|
function hook_comment_view_alter(&$build, \Drupal\comment\Entity\Comment $comment, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
|
2009-11-07 13:35:21 +00:00
|
|
|
// Check for the existence of a field added by another module.
|
2009-12-26 16:50:09 +00:00
|
|
|
if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
|
2009-11-07 13:35:21 +00:00
|
|
|
// Change its weight.
|
|
|
|
$build['an_additional_field']['#weight'] = -10;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a #post_render callback to act on the rendered HTML of the comment.
|
|
|
|
$build['#post_render'][] = 'my_module_comment_post_render';
|
|
|
|
}
|
|
|
|
|
2009-01-04 16:10:48 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Respond to a comment being published by a moderator.
|
2009-01-04 16:10:48 +00:00
|
|
|
*
|
2013-10-03 11:26:25 +00:00
|
|
|
* @param \Drupal\comment\Comment $comment
|
2012-01-03 04:36:15 +00:00
|
|
|
* The comment the action is being performed on.
|
2009-01-04 16:10:48 +00:00
|
|
|
*/
|
2012-05-03 05:49:41 +00:00
|
|
|
function hook_comment_publish(Drupal\comment\Comment $comment) {
|
2013-01-14 10:59:47 +00:00
|
|
|
drupal_set_message(t('Comment: @subject has been published', array('@subject' => $comment->subject->value)));
|
2009-01-04 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Respond to a comment being unpublished by a moderator.
|
2009-01-04 16:10:48 +00:00
|
|
|
*
|
2013-10-03 11:26:25 +00:00
|
|
|
* @param \Drupal\comment\Comment $comment
|
2012-01-03 04:36:15 +00:00
|
|
|
* The comment the action is being performed on.
|
2009-01-04 16:10:48 +00:00
|
|
|
*/
|
2012-05-03 05:49:41 +00:00
|
|
|
function hook_comment_unpublish(Drupal\comment\Comment $comment) {
|
2013-01-14 10:59:47 +00:00
|
|
|
drupal_set_message(t('Comment: @subject has been unpublished', array('@subject' => $comment->subject->value)));
|
2009-01-04 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-17 12:43:37 +00:00
|
|
|
* Act before comment deletion.
|
|
|
|
*
|
2013-09-01 06:20:08 +00:00
|
|
|
* This hook is invoked from entity_delete_multiple() before field values are
|
|
|
|
* deleted and before the comment is actually removed from the database.
|
2009-01-04 16:10:48 +00:00
|
|
|
*
|
2013-10-03 11:26:25 +00:00
|
|
|
* @param \Drupal\comment\Comment $comment
|
2011-12-17 12:43:37 +00:00
|
|
|
* The comment object for the comment that is about to be deleted.
|
|
|
|
*
|
|
|
|
* @see hook_comment_delete()
|
|
|
|
* @see entity_delete_multiple()
|
|
|
|
*/
|
2012-05-03 05:49:41 +00:00
|
|
|
function hook_comment_predelete(Drupal\comment\Comment $comment) {
|
2011-12-17 12:43:37 +00:00
|
|
|
// Delete a record associated with the comment in a custom table.
|
|
|
|
db_delete('example_comment_table')
|
2013-01-14 10:59:47 +00:00
|
|
|
->condition('cid', $comment->id())
|
2011-12-17 12:43:37 +00:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to comment deletion.
|
|
|
|
*
|
2013-09-01 06:20:08 +00:00
|
|
|
* This hook is invoked from entity_delete_multiple() after field values are
|
|
|
|
* deleted and after the comment has been removed from the database.
|
2011-12-17 12:43:37 +00:00
|
|
|
*
|
2013-10-03 11:26:25 +00:00
|
|
|
* @param \Drupal\comment\Comment $comment
|
2011-12-17 12:43:37 +00:00
|
|
|
* The comment object for the comment that has been deleted.
|
|
|
|
*
|
|
|
|
* @see hook_comment_predelete()
|
|
|
|
* @see entity_delete_multiple()
|
2009-01-04 16:10:48 +00:00
|
|
|
*/
|
2012-05-03 05:49:41 +00:00
|
|
|
function hook_comment_delete(Drupal\comment\Comment $comment) {
|
2013-01-14 10:59:47 +00:00
|
|
|
drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->subject->value)));
|
2009-01-04 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
2013-11-29 09:40:59 +00:00
|
|
|
/**
|
|
|
|
* Alter the links of a comment.
|
|
|
|
*
|
|
|
|
* @param array &$links
|
|
|
|
* A renderable array representing the comment links.
|
|
|
|
* @param \Drupal\comment\CommentInterface $entity
|
|
|
|
* The comment being rendered.
|
|
|
|
* @param array &$context
|
|
|
|
* Various aspects of the context in which the comment links are going to be
|
|
|
|
* displayed, with the following keys:
|
|
|
|
* - 'view_mode': the view mode in which the comment is being viewed
|
|
|
|
* - 'langcode': the language in which the comment is being viewed
|
|
|
|
* - 'commented_entity': the entity to which the comment is attached
|
|
|
|
*
|
|
|
|
* @see \Drupal\comment\CommentViewBuilder::renderLinks()
|
|
|
|
* @see \Drupal\comment\CommentViewBuilder::buildLinks()
|
|
|
|
*/
|
|
|
|
function hook_comment_links_alter(array &$links, CommentInterface $entity, array &$context) {
|
|
|
|
$links['mymodule'] = array(
|
|
|
|
'#theme' => 'links__comment__mymodule',
|
|
|
|
'#attributes' => array('class' => array('links', 'inline')),
|
|
|
|
'#links' => array(
|
|
|
|
'comment-report' => array(
|
|
|
|
'title' => t('Report'),
|
|
|
|
'href' => "comment/{$entity->id()}/report",
|
|
|
|
'html' => TRUE,
|
|
|
|
'query' => array('token' => \Drupal::getContainer()->get('csrf_token')->get("comment/{$entity->id()}/report")),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-11-25 02:37:33 +00:00
|
|
|
/**
|
|
|
|
* @} End of "addtogroup hooks".
|
|
|
|
*/
|