You are here

function entityform_views_query_alter in Entityform 7

Same name and namespace in other branches
  1. 7.2 entityform.module \entityform_views_query_alter()

Implements hook_views_query_alter().

Control access to Entityforms in Views. This will only work if entityform is the base table of the View

File

./entityform.module, line 1673
Module for the Entityform Entity - a starting point to create your own Entity and associated administration interface

Code

function entityform_views_query_alter(&$view, &$query) {
  global $user;

  // Check for 3 conditions
  // 1. 'user_access' tag is on query
  // 2. Base table is 'entityform'
  // 3. User doesn't have 'view any entityform'
  if (!empty($view->query->tags) && in_array('user_access', $view->query->tags) && $view->base_table == 'entityform' && !user_access('view any entityform')) {

    // @todo user $query to make sure this is the alias
    $table_alias = 'entityform';
    if (user_access('view own entityform')) {

      //Make sure View only returns entityforms for current User
      $uid = $user->uid;
    }
    else {

      //Provide uid that will never match because

      //the current user has no permission to view Entityforms.

      //The permission should have be implemented on the View but just in case.
      $uid = -1;
    }
    $query->where[] = array(
      'conditions' => array(
        array(
          'field' => "{$table_alias}.uid",
          'value' => $uid,
          'operator' => '=',
        ),
      ),
      'type' => 'AND',
      'args' => array(),
    );
  }
}