2008-11-25 02:37:33 +00:00
|
|
|
<?php
|
|
|
|
|
2012-09-12 09:18:04 +00:00
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
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
|
|
|
*
|
2012-05-03 05:49:41 +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
|
|
|
*
|
2012-05-03 05:49:41 +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-02-01 16:08:58 +00:00
|
|
|
search_touch_node($comment->nid->target_id);
|
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
|
|
|
*
|
2012-05-03 05:49:41 +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-02-01 16:08:58 +00:00
|
|
|
search_touch_node($comment->nid->target_id);
|
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.
|
|
|
|
*
|
|
|
|
* @param \Drupal\comment\Plugin\Core\Entity\Comment $comment
|
|
|
|
* The comment object.
|
|
|
|
*/
|
|
|
|
function hook_comment_create(\Drupal\comment\Plugin\Core\Entity\Comment $comment) {
|
|
|
|
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-01-08 19:16:16 +00:00
|
|
|
* @param \Drupal\comment\Plugin\Core\Entity\Comment $comment $comment
|
2009-01-04 16:10:48 +00:00
|
|
|
* Passes in the comment the action is being performed on.
|
2013-01-08 19:16:16 +00:00
|
|
|
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
|
|
|
|
* 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-01-08 19:16:16 +00:00
|
|
|
function hook_comment_view(\Drupal\comment\Plugin\Core\Entity\Comment $comment, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display, $view_mode, $langcode) {
|
|
|
|
// 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-01-08 19:16:16 +00:00
|
|
|
* @param \Drupal\comment\Plugin\Core\Entity\Comment $comment
|
2012-06-15 16:43:34 +00:00
|
|
|
* The comment being rendered.
|
2013-01-08 19:16:16 +00:00
|
|
|
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
|
|
|
|
* 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-01-08 19:16:16 +00:00
|
|
|
function hook_comment_view_alter(&$build, \Drupal\comment\Plugin\Core\Entity\Comment $comment, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $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
|
|
|
*
|
2012-05-03 05:49:41 +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
|
|
|
*
|
2012-05-03 05:49:41 +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-06-11 16:38:12 +00:00
|
|
|
* This hook is invoked from entity_delete_multiple() before
|
2011-12-17 12:43:37 +00:00
|
|
|
* field_attach_delete() is called and before the comment is actually removed
|
|
|
|
* from the database.
|
2009-01-04 16:10:48 +00:00
|
|
|
*
|
2012-05-03 05:49:41 +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-06-11 16:38:12 +00:00
|
|
|
* This hook is invoked from entity_delete_multiple() after
|
2011-12-17 12:43:37 +00:00
|
|
|
* field_attach_delete() has called and after the comment has been removed from
|
|
|
|
* the database.
|
|
|
|
*
|
2012-05-03 05:49:41 +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
|
|
|
}
|
|
|
|
|
2008-11-25 02:37:33 +00:00
|
|
|
/**
|
|
|
|
* @} End of "addtogroup hooks".
|
|
|
|
*/
|