1

I want an user configurable boolean view filter, displaying child nodes in an hierarchy based on a value, canDisplayOthers, in the master node. The master/children are joined by a field setID.

I tried to do a self-join, adding a view relationship based on setID, but setID is not in the list of relationship fields.

Using Php, the recipe in:

https://ohthehugemanatee.org/blog/2013/12/26/44497-people-are-wrong-how-to-never-use-views-php/

says to use a handler. My handler would return a boolean, something like:

class mymodule_handler_field_isDisplayable extends views_handler_field {
  /**
  * Render the name field.
  */
  public function render($values) {
    $value = $this->get_value($values);
    /*   
       How can I get current node ID (nid) here?

    */
  }
}

and a hook_views_data something like:

function mymodule_views_data() {
  // Basic table information.
  $data['mymodule_ToDisplay'] = array(
    'table' => array(
      'group' => t('Global'),
      'base' => array(
        'title' => t('ToDisplay')
        ),
      ),
    'name' => array(
      'title' => t('ToDisplay'),
      'field' => array(
        'handler' => 'mymodule_handler_field_isDisplayable',
      )
    ),
    'filter' => array(
    'handler' => 'views_handler_filter_boolean',
    )
  ); 
  return $data;
}

I can't figure out:

  1. in mymodule_views_data, what is this thing called 'table'?
  2. how do I get the setID or current nodeID into the mymodule_handler_field_isDisplayable function?

In curiosity; - what is the minimal hook_views_data? - Where is a good place to find where hook_views* parameters are explained?

I am using drupal7 and views 3

ADDITION 15 July 2014

Geoff, below, kindly mentioned that the problem would be easier if I created a master node content type. I created a masterNodeType content type, containing the fields:

  • setID
  • canDisplay

Now, masterNodeType and childNodeType have the common field, setID, but I can't see setID when I configure the view relationship. How do I get setID, and/or masterNodeType into the view relationship? What is the criteria for those view relationship components?

  • If I understand correctly, you have created a hierarchical setup with master nodes being tagged as master, and you want a view to show only rows of the content type where the master canDisplayOthers is set, for any given setID? – Geoff Jul 7 '14 at 15:02
  • This is exactly what I want. – Dennis Jul 14 '14 at 5:37
  • Regarding documentation, some comes with Views itself. Download and enable advanced_help and check out the help topics for Views' API and its children. – Andy Jul 14 '14 at 9:52
  • One problem I see with this, is that you would need to cycle through ALL nodes of that content type either after a query is run, or worse yet, before the query is run, in order to determine if it's the master, and then adjust the next query to suit it's setID. have you considered using a separate content type for master? would be much more efficient and easier to manage I think. – Geoff Jul 14 '14 at 13:10
  • Master content type is a great idea, thanks! I created a separate content type for the master node, but I still cannot see it when I try to create the relationship. Also, thanks for the pointer to the documentation. – Dennis Jul 15 '14 at 12:16
0

I'm not sure if this is the best solution, but it is fairly easy and code free. I needed to display some "associated nodes" for a certain content type and so I added the views_field_view project (https://www.drupal.org/project/views_field_view), a module that lets you embed one view into another. In this case, you can create a view of nodes, formatted as you like, and display it in the master view, and it will always sync up related data.

For an explanation of how to use this great little project, see: http://www.webomelette.com/view-within-view-fields-module https://www.youtube.com/watch?v=dUoL_ARPLcI (Drupal 6 but still helpful)

0

Thanks to the clues provided by Geoff above, I found the answer.

My mistake, (doing too much work), was to create my own foreign key, test_set_id. Drupal uses node_reference fields for such joins. Using node reference, associated content can be directly accessed after adding the relationship in the set-up view screen, without any code.

I also ended up creating an additional master parent content type, to hold some generic test_set information, and to give the child nodes something to point to.

Thanks for all the help!

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.