0

1) I created a personal "Flags" called "Follow" on the nodes.

2) I created a page view (comment) that displays the comments of the flagged nodes.

So far everything works.

Now I want comments to be filtered with the creation date of the flags.

For example :

  • A node with 5 comments.
  • I flick the neoud at 10 am on the first of February.
  • On February 2nd, there are 3 new comments.

How to display in my view, only the comments published after the date of the flag on the node?

So only display the 3 comments.

The solution would be to add the date of publication of the comment and replace it with the date of creation of the flag.

How to do this ? Can we do this with a context filter ?

UPDATE

The machine name of my view is notify_node_new_comment

The machine name of my flag is like_node

I created a folder comment_notify Here are the two files I added :

comment_notify.info.yml :

name: Comment notify
description: Notification of new comments.
package: Custom

type: module
core: 8.x

comment_notify.module :

/**
 * Implements hook_views_pre_render().
 */
<?php
function comment_notify_views_pre_render(Drupal\views\ViewExecutable $view) {
  // get the machine name of your view.
  // var_dump($view->id()); die('stop');
  if ($view->id() === 'notify_node_new_comment') {
    // get the node id from the views argument.
    $nid = $view->args[0];
    $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
    $connection = \Drupal::database();
    $query = $connection->query("SELECT last_updated FROM {flag_counts} WHERE entity_id = :entity_id" , [
      ':entity_id' => $nid,
    ]);
    // if more flag types add to above 
    ':flag_id' => 'like_node',
    $result = $query->fetchAll();
    if (!empty($result[0]->last_updated)) {
      $flagged_date = $result[0]->last_updated;
      if (count($view->result) != 0)  {
        // you can include the dynamic cache page killer.
        // \Drupal::service('page_cache_kill_switch')->trigger();
        $new_result = [];
        foreach ($view->result as $key => $row) {
          $comment = $row->_entity;
          $created = $comment->created->value;
          if ($created > $flagged_date) {
            $new_result[] = $row;
          }
        }
        $view->result = $new_result;
      }
    }
  }
}

enter image description here

The custom module installs, but the following error is displayed in the log :

ParseError: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in Drupal\Core\Extension\Extension->load() (line 17 of /home/www.s1biose.com/public_html/web/modules/custom/comment_notify/comment_notify.module) #0 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(127): Drupal\Core\Extension\Extension->load() #1 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/Extension/ModuleInstaller.php(186): Drupal\Core\Extension\ModuleHandler->load('comment_notify') #2 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php(83): Drupal\Core\Extension\ModuleInstaller->install(Array, true) #3 /home/www.s1biose.com/public_html/web/core/modules/system/src/Form/ModulesListForm.php(448): Drupal\Core\ProxyClass\Extension\ModuleInstaller->install(Array) #4 [internal function]: Drupal\system\Form\ModulesListForm->submitForm(Array, Object(Drupal\Core\Form\FormState)) #5 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/Form/FormSubmitter.php(111): call_user_func_array(Array, Array) #6 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/Form/FormSubmitter.php(51): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState)) #7 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/Form/FormBuilder.php(585): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState)) #8 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/Form/FormBuilder.php(314): Drupal\Core\Form\FormBuilder->processForm('system_modules', Array, Object(Drupal\Core\Form\FormState)) #9 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/Controller/FormController.php(74): Drupal\Core\Form\FormBuilder->buildForm('system_modules', Object(Drupal\Core\Form\FormState)) #10 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch)) #11 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #12 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() #13 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #14 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #15 [internal function]: Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() #16 /home/www.s1biose.com/public_html/vendor/symfony/http-kernel/HttpKernel.php(153): call_user_func_array(Object(Closure), Array) #17 /home/www.s1biose.com/public_html/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #18 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #19 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #20 /home/www.s1biose.com/public_html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #21 /home/www.s1biose.com/public_html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #22 /home/www.s1biose.com/public_html/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #23 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\ban\BanMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #24 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #25 /home/www.s1biose.com/public_html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #26 /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/DrupalKernel.php(657): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #27 /home/www.s1biose.com/public_html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #28 {main}.

0

In your view "comment" look in to adding a RELATIONSHIPS Content flag when your view has this relationship, you can then add many filter criteria that becomes able to use that were not available before.

relationship flag

UPDATE:

Since there really is no default filter to do this a quicker win is just strip values form the views output - as i was going to do a custom view filter but got too complex for this example.

So you need to create or append to a custom module,

Assumption: you are using a view not the default form display of the comments.

/**
 * Implements hook_views_pre_render().
 */
function YOURMODULE_views_pre_render(Drupal\views\ViewExecutable $view) {
  // get the machine name of your view.
  // var_dump($view->id()); die('stop');
  if ($view->id() === 'my_view_machine_name') {
    // get the node id from the views argument.
    $nid = $view->args[0];
    $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
    $connection = \Drupal::database();
    $query = $connection->query("SELECT last_updated FROM {flag_counts} WHERE entity_id = :entity_id" , [
      ':entity_id' => $nid,
    ]);
    // if more flag types add to above 
    ':flag_id' => 'your_flag_machine_name',
    $result = $query->fetchAll();
    if (!empty($result[0]->last_updated)) {
      $flagged_date = $result[0]->last_updated;
      if (count($view->result) != 0)  {
        // you can include the dynamic cache page killer.
        // \Drupal::service('page_cache_kill_switch')->trigger();
        $new_result = [];
        foreach ($view->result as $key => $row) {
          $comment = $row->_entity;
          $created = $comment->created->value;
          if ($created > $flagged_date) {
            $new_result[] = $row;
          }
        }
        $view->result = $new_result;
      }
    }
  }
}

and the view has a Comment: Entity ID "contextual filter" so you can use the view argument to get the node id.

$nid = $view->args[0];

aka

enter image description here

you have :

UPDATE # 4;

you have an error in your php

$query = $connection->query("SELECT last_updated FROM {flag_counts} WHERE entity_id = :entity_id" , [
':entity_id' => $nid,
 ]);
 // if more flag types add to above 
 ':flag_id' => 'like_node',

THIS SHOULD BE....

$query = $connection->query("SELECT last_updated FROM {flag_counts} WHERE entity_id = :entity_id AND flag_id = :flag_id" , [
':entity_id' => $nid,
':flag_id' => 'like_node',
]);

sorry i really thought you had some understanding of php Best of luck.

  • Thank you, but that does not answer my question. I have flagged a node, I want to display the comments published after the date of the flag which are associated with the node. – user82735 Jan 31 '18 at 5:48
  • Yep it does not really anwser but with that relationship then you can add a filter criterion: Flagging: Last flagging , Operator = more than some date , then you can hook_views_pre_execute and change the enterd date to the flag date ! which will give all comments after that date ! – Taggart Jensen Jan 31 '18 at 6:01
  • I'm interested, but I do not know how to code. How to do this ? Can you put an answer with an example ? – user82735 Jan 31 '18 at 6:03
  • updated the anwser – Taggart Jensen Feb 1 '18 at 4:10
  • thank you for your reply. I do not know the custom modules, it seems complex. Is there a solution from the drupal interface or is there a module to allow this? I found this module drupal.org/project/views_arguments but it does not exist for drupal 8 – user82735 Feb 1 '18 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy