This module allows you to add a new custom field in the search API index for node view count.

Requirements

This module requires the Search API module. This module node count values are dependent on the statistics(core) module.so before using this module we need to enable the statistics module.

Project Link

https://www.drupal.org/project/searchapi_nodeview_count

Git instructions

git clone --branch 1.0.x https://git.drupalcode.org/project/searchapi_nodeview_count.git

Comments

drupal-ramesh created an issue. See original summary.

avpaderno’s picture

Issue summary: View changes

Thank you for applying! Remember to change status, as the current one is used to say to the reviewers the project is not ready to be reviewed.

avpaderno’s picture

Issue summary: View changes
avpaderno’s picture

drupal-ramesh’s picture

Status: Active » Needs review
batkor’s picture

Status: Needs review » Needs work

Hi
1. Add check entity type this line https://git.drupalcode.org/project/searchapi_nodeview_count/-/blob/1.0.x...
#

if ($datasource
      && $datasource->getEntityTypeId()
      && $datasource->getEntityTypeId() === 'node') {

2. Code standard https://git.drupalcode.org/project/searchapi_nodeview_count/-/blob/1.0.x...
3. Please use DI
https://git.drupalcode.org/project/searchapi_nodeview_count/-/blob/1.0.x...
#

/**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->statisticStorage = $container->get('statistics.storage.node');
    return $instance;
  }
... ... ...
 /**
   * {@inheritdoc}
   */
  public function addFieldValues(ItemInterface $item) {
  ... ...
$result = $this->statisticStorage->fetchView($entity->id())

... ...
fields = $this->getFieldsHelper()
        ->filterForPropertyPath($fields, 'entity:node', 'node_viewscount'); 
... ...
}
kuldeep_mehra27’s picture

Thank you for the contribution!

Please fix the Drupal coding standards.

FILE: /var/www/html/nutrition-tracker/web/modules/contrib/searchapi_nodeview_count/src/Plugin/search_api/processor/NodeViewsCount.php
---------------------------------------------------------------------------------------------------------------------------------------------
FOUND 9 ERRORS AFFECTING 5 LINES
---------------------------------------------------------------------------------------------------------------------------------------------
49 | ERROR | [x] Whitespace found at end of line
49 | ERROR | [x] Functions must not contain multiple empty lines in a row; found 2 empty lines
50 | ERROR | [x] Whitespace found at end of line
51 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
51 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 3
57 | ERROR | [x] No space found before comment text; expected "// get node's user views count using statistics module" but found "//get
| | node's user views count using statistics module"
57 | ERROR | [x] Inline comments must start with a capital letter
57 | ERROR | [x] Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses
59 | ERROR | [x] Expected 1 space after IF keyword; 0 found
---------------------------------------------------------------------------------------------------------------------------------------------

kuldeep_mehra27’s picture

------ ----------------------------------------------------------------------------------------------
Line src/Plugin/search_api/processor/NodeViewsCount.php
------ ----------------------------------------------------------------------------------------------
Class Drupal\search_api\Processor\ProcessorPluginBase not found and could not be autoloaded.
24 Class Drupal\search_api\Processor\ProcessorPluginBase not found and could not be autoloaded.
------ ----------------------------------------------------------------------------------------------

drupal-ramesh’s picture

Hi
Thank you for valuable feedback. I have fixed above coding issues and used DI in plugin.
@kuldeep_mehra27 - kindly let me know that when you got pluginbase not found error.

drupal-ramesh’s picture

Status: Needs work » Needs review
batkor’s picture

Status: Needs review » Needs work

Remove this condition https://git.drupalcode.org/project/searchapi_nodeview_count/-/blob/1.0.x...
You already checked https://git.drupalcode.org/project/searchapi_nodeview_count/-/blob/1.0.x...

$views_count not exists https://git.drupalcode.org/project/searchapi_nodeview_count/-/blob/1.0.x...
Change code to for example

if ($result = $this->statisticStorage->fetchView($entity->id())) {
$fields = $item->getFields(FALSE);
    $fields = $this->getFieldsHelper()
      ->filterForPropertyPath($fields, 'entity:node', 'node_viewscount');
    foreach ($fields as $field) {
      $field->addValue($result->getTotalCount());
    }

}
drupal-ramesh’s picture

@batkor above changes are updated in dev version.

drupal-ramesh’s picture

Status: Needs work » Needs review
batkor’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me now.

klausi’s picture

Status: Reviewed & tested by the community » Fixed

Looks good to me!

Thanks for your contribution, Ramesh!

The module is too short to approve you as git vetted user, but I have opted into security coverage for you on your project. Please get back to us once you have another project to promote.

Here are some recommended readings to help with excellent maintainership:

You can find lots more contributors chatting on Slack or IRC in #drupal-contribute. So, come hang out and stay involved!

Thanks, also, for your patience with the review process. Anyone is welcome to participate in the review process. Please consider reviewing other projects that are pending review. I encourage you to learn more about that process and join the group of reviewers.

Thanks to the dedicated reviewer(s) as well.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.