2

I'm trying to create a custom views access plugin, so I can perform my conditions for accessing a view.

I read several tutorials and checked the source of existing modules, but I must be coding/doing something wrong because this never works properly.

Here's the code:

File: sample.module:

function sample_views_api() {
  return array(
    'api' => 3,
  );
}

File: sample.views.inc:

function sample_views_plugins() {
  $plugins = array(
    'access' => array(
      'test' => array(
        'title' => t('Custom access check'),
        'help' => t('This is a custom access plugin.'),
        'handler' => 'sample_access_plugin',
      ),
    ),
  );
  return $plugins;
}

class sample_access_plugin extends views_plugin_access {
  function summary_title() {
    return t('Custom acces plugin');
  }
  function access($account) {
    return sample_access($account);
  }
  function get_access_callback() {
    return array('sample_access', array());
  }
}

function sample_access($account = NULL) {
  return TRUE;
}

I can see the access plugin in views, and I can select it. However, no matter what I do, the access to the view will always be forbidden (access denied). There are no messages in the error log.

Any ideas what's wrong here?

Thanks!

2 Answers 2

2
+50

It seems like the sample.views.inc file does not get loaded during the access-check (as the registry already cached the information which callback it needs to execute).

So you can just move sample_access() to the file sample.module and the callback works.

2
  • Thanks for the response, will check this and keep you posted! Jul 31, 2015 at 15:07
  • THANKS A LOT! That was it - it's working properly now :) Aug 2, 2015 at 13:43
2

As noted on the documentation for hook_views_plugins():

handler (required): The name of the file containing the class describing the handler, which must also be the name of the handler's class.

So you should put sample_access_plugin class in a file called sample_access_plugin.inc, and note also:

path: Path to the handler. Only required if the handler is not placed in the same folder as the .module file or in the subfolder 'views'.

I believe that sample_access_plugin.inc also needs to be listed in the files[] section of your module's info file in order for the class to be autoloaded.

1
  • Thanks for the message; the access plugin is already listed in the files[] section. The solution was what Nebel54 said - the access callback function needs to be loaded always, meaning it should either be in the .module file or some other file that is always present. Aug 2, 2015 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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