If you want to create a custom views access plugin the reason for this will be to allow you to have an entry point to add custom logic to protect the access to your view.
Let say you need a finer access control to a view than roles and permissions. For example you are storing properties on a user object who determine if certain user can see certain views independent of their roles.
What we need to do, is to configure our views correctly with a views access plugin.
The hacky way:
You could load the user in the template file of your view and check for the property but this is not what we want to do. While this would work you would have to redo it in every template and you will not be able to configure it. It is also difficult to control. Its hacky and hacky solutions tend to cause a lot of maintenance. Making these quick solutions costing more in the long run. This is worth a different blog post entirely.
Here is the drupal way:
Views has this beautiful architecture in place that allows you to extend its base classes. If you start writing a custom views plugin the best thing to do is to examine an other plugin of its kind. For example check out the permissions access plugin. (tip: a good way to write plugins is to look at examples from views or other module implementing the same system)
The file:
/views/plugins/views_plugin_access_perm.inc
<?php /** * @file * Definition of views_plugin_access_perm. */ /** * Access plugin that provides permission-based access control. * * @ingroup views_access_plugins */ class views_plugin_access_perm extends views_plugin_access { function access($account) { return views_check_perm($this->options['perm'], $account); } function get_access_callback() { return array('views_check_perm', array($this->options['perm'])); } function summary_title() { $permissions = module_invoke_all('permission'); if (isset($permissions[$this->options['perm']])) { return $permissions[$this->options['perm']]['title']; } return t($this->options['perm']); } function option_definition() { $options = parent::option_definition(); $options['perm'] = array('default' => 'access content'); return $options; } function options_form(&$form, &$form_state) { parent::options_form($form, $form_state); $perms = array(); $module_info = system_get_info('module'); // Get list of permissions foreach (module_implements('permission') as $module) { $permissions = module_invoke($module, 'permission'); foreach ($permissions as $name => $perm) { $perms[$module_info[$module]['name']][$name] = strip_tags($perm['title']); } } ksort($perms); $form['perm'] = array( '#type' => 'select', '#options' => $perms, '#title' => t('Permission'), '#default_value' => $this->options['perm'], '#description' => t('Only users with the selected permission flag will be able to access this display. Note that users with "access all views" can see any view, regardless of other permissions.'), ); } }
Create a file called custommodule_access_plugin.inc in the root of your custom module.
<?php /** * Access plugin that provides property based access control. */ class custommodules_access_plugin extends views_plugin_access { function summary_title() { return t('Custom acces plugin'); } // summary_title() /** * Determine if the current user has access or not. */ function access($account) { return custommodules_access($account); } function get_access_callback() { return array('custommodules_access', array()); } }
Then we have our access method where you the custom callback. Finaly you must declare to views your custom access callback. You can pass arguments if need using the array see in the example above.
We have our file with the class now we need to tell views that we have created a new access plugin.
Two things need to be done. First in the info file you must tell there is a file containing the class.
name = custommodule description = Custom code core = 7.x package = custom dependencies[] = views files[] = custommodule_access_plugin.inc
/** * Implements hook_views_plugins() */ function custommodule_views_plugins() { $plugins = array( 'access' => array( 'test' => array( 'title' => t('Custom Access check'), 'help' => t('this is a custom access plugin'), 'handler' => 'custommodule_access_plugin', 'path' => drupal_get_path('module', 'custommodule'), ), ), ); return $plugins; }
function custommodules_access($account = NULL) { global $user; $access = false; $account = user_load($user->uid); $optionfield = field_get_items('user', $account, 'field_option'); //In the future more values are possible so this is extendible //For now only +eur exists $allowed_values = array('eur'); $options = explode('+', $optionfield[0]['value']); foreach ($allowed_values as $allowed_value) { if (in_array($allowed_value, $options)) { $access = true; } } return $access; }
Now that you have this plugin you can start using it in all your views by going to the interface access section and select the plugin.
Original source http://dominiquedecooman.com/drupal-7-tip-create-custom-views-access-plugin