1

I am totally new to drupal 8 I have custom view field plugin created to render the user type permission(anonymous or authenticated) for each node displayed in view.

for that i used the content access module to get permission for any node.. here's the code

/** 
*
* @ingroup views_field_handlers
*
* @ViewsField("node_access_level")
*/
class NodeAccessLevel extends FieldPluginBase {
  public function usesGroupBy() {
  return FALSE;
}
public function query() {
 // Do nothing -- to override the parent query.
}
protected function defineOptions() {
  $options = parent::defineOptions();
  $options['hide_alter_empty'] = ['default' => FALSE];
  return $options;
} 
public function buildOptionsForm(&$form, FormStateInterface $form_state)          
{
  parent::buildOptionsForm($form, $form_state);
}
public function render(ResultRow $values) {
 $id = $values->_entity->id();
 $nid = Node::load($id);
 $operation = 'view';
 $roles = content_access_get_rids_per_node_op($operation, $nid);
 $user_roles = Role::loadMultiple();
 $role_names = array_map(function ($role) use ($user_roles) {
  return $role == 'administrator' ? '' : $user_roles[$role]- >get('label');
 }, $roles);
  return implode('/', array_filter($role_names));
 }
}

Now this works perfectly ...and gives me a node access filed in every view and display the the access in column i specify. now i want to create a exposed filter for it.. i have updated the hook view_data and using this code..

/**
* Filter content types specified in page manager config 
*
* @ViewsFilter("node_access_level_filter")
*/
class NodeAccessLevelFilter extends ManyToOne {
  public function init(ViewExecutable $view, DisplyPluginBase $display,  array &$options = NULL){
  parent::init($view, $display, $options);
  $this->valueTitle = 'Allowed event titles';
  $this->definition['options callback'] = array($this, 'getOptions');
 }
 /** 
 * Helper function that generates the options.
 * @return array
 */
 public function getOptions(){
  return array('Public' => 'Public', 'Logged In'=>'Logged In');
  } 
 } 

the filter is field is available and i have the desired dropdown.. But ut doesn't filter the content. Doesn't the filter works on same title.Also does my filter know which field to filter on. How can i do that.. Can any1 help. do i need to update some query or what to do.

| improve this question | | | | |

Your Answer

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

Browse other questions tagged or ask your own question.