1

So I have already read and followed everything I could find on that issue:

  1. putting my_module_views_data_alter(array &$data) in my_module.views.inc file in the root directory of my_module,
  2. disabling cache for my view,
  3. clearing caches million times,

but still I can never stop at a breakpoint inside this hook, nor can I see any effects of the code I have put in it.

What am I missing?

EDIT: updating with some code

In my_module I have already created a computed field user_age, calculating user age from a custom user profile field with date of birth.

my_module/my_module.module

function my_module_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'user') {
    $fields = [];
    $fields['user_age'] = BaseFieldDefinition::create('user_age_field')
      ->setName('user_age')
      ->setLabel(t('User age'))
      ->setComputed(TRUE)
      ->setClass('\Drupal\my_module\UserAgeIntegerItemList')
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'weight' => -5,
      ]);

    return $fields;
  }
}
function my_module_field_formatter_info_alter(array &$info) {
  $info['number_integer']['field_types'][] = 'user_age_field';
}

This field is working and is accessible in my user profile template.

Knowing that since Drupal 8.4

Computed fields can now be displayed by Views

I wanted to use field user_age as an exposed filter in my search_users view:

my_module/my_module.views.inc

/**
 * Implements hook_views_data_alter().
 */
function my_module_views_data_alter(array &$data) {
  if (isset($data['users_field_data'])) {
    $data['users_field_data']['user_age'] = [
      'title' => t('User age'),
      'field' => [
        'id' => 'my_module_view_user_age',
      ],
    ];
  }
}

my_module/src/Plugin/views/field/MyModuleViewUserAge.php

<?php

namespace Drupal\my_module\Plugin\views\field;

use Drupal\views\ResultRow;
use Drupal\views\Plugin\views\field\FieldPluginBase;

/**
 * A handler to provide proper displays for user user_age.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("my_module_view_user_age")
 */
class MyModuleViewUserAge extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $relationship_entities = $values->_relationship_entities;
    $age = '';
    // First check the referenced entity.
    if (isset($relationship_entities['user'])) {
      $user = $relationship_entities['user'];
    }
    else {
      $user = $values->_entity;
    }

    $type = get_class($user);
    if ($type === 'Drupal\user\Entity\User') {
      $age = $user->get('user_age')->getvalue();
    }

    return $age;
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    // This function exists to override parent query function.
    // Do nothing.
  }
}

but my_module_views_data_alter() seems to never be executed and user_age field is not present in the filter criteria for my view.

  • 1
    It doesn't need to go into a .view.inc file. Try placing your code in my_module.module and flushing the caches – Leigh Jan 23 at 10:40
  • It went there after being in my_module.module first and not working there either :) – KrzysiekK Jan 23 at 10:43
  • either place your code to my_module.module or just load your .inc file inside your module like module_load_include('inc', 'my_module', 'my_module.views'); – berramou Jan 23 at 10:45
  • Have you enabled your custom module? An easy thing to forget if you're trying new things out – Leigh Jan 23 at 10:47
  • my_module is enabled. This hook was not working in .module file, and is not working in .inc loaded with module_load_include. But if I add a random function to my .inc file I can stop at a breakpoint in it. Just not at the breakpoint in hook_views_data_alter(). When exactly is this hook supposed to fire? – KrzysiekK Jan 23 at 11:26
0

I'm really interested in this answer because I'm having the exact same problem with Drupal 8.6.4 and can't get my hook to be called. It is called when I clear the cache, though, so I know my module/hook is working. It just isn't called when the view is displayed.

  • My hook isn't even called on cache clearing. Where did you put yours? – KrzysiekK Feb 6 at 9:24
  • Same problem. :) – Marko Blazekovic May 28 at 14:17
0

You can't xdebug in hook itself, you need to go to method calling this hooks which is in class ViewsData path /core/modules/views/src/ViewsData.php and put breakpoint in getData, clear cache and you will get what you need.

-1

Here is an example MYMODULE.views.inc I've used recently in a module:

/**
 * Implements hook_views_data().
 */
function MYMODULE_views_data() {

  $data['schema_name']['nid'] = [
    'title' => t('NID'),
    'help' => t('NID from schema_name'),
    'field' => [
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
      'float' => TRUE,
      'id' => 'numeric',
    ],
    'relationship' => [
      'title' => 'NID',
      'label' => t('NID'),
      'help' => t('NID from schema_name'),
      'id' => 'standard',
      'base' => 'node',
      'base field' => 'nid',
      'relationship field' => 'nid',
      'handler' => 'views_handler_relationship',
    ],
    'sort' => [
      'id' => 'standard',
    ],
    'filter' => [
      'id' => 'numeric',
    ],
    'argument' => [
      'id' => 'numeric',
    ],
  ];

  return $data;

}

Some things I don't see in your code:

  1. no handler for the views field
  2. no filter or sort key for your field
  3. not printing return $data; in your views.inc file

I know this is not a direct answer, but hoping it helps.

  • The handler was the last part of the code in my question. hook_views_data_alter() uses &$data, so no returns there, and I did not use hook_views_data(), because I don't think I need to define any relationships for my field at this point (or do I, because otherwise I will not get this running at all?). Even if I use hook_views_data() it still won't ever stop at a breakpoint in it. – KrzysiekK Jan 24 at 8:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.