• vote
    22
    0 starsmike | Shared With: Everyone - Mar 28 2008 | drupal, views, database
    context-sensitive embedded views | drupal.org

    I'm trying to embed a (static) view in a Drupal node (I want to show the employees of a company inside the company node).

    This is the first time I've found that there seems to be no Drupal module for doing a common action like this. The two related modules (Insert View and View Field) allow the user to embed a view at edit time of the node. I just want this view statically defined for the node.

    It seems pretty insane to have to embed custom PHP for each node that you want to contain a sub-view.

    Showing 1 - 1 of 1 comments
    • mike - Mar 30 2008

      There is a small amount of PHP required to do this. The basic technique is to create a sub-view which you will display when the parent node is displayed. But, in order to get the correct filter, you have to add an "argument" to the sub-view using the CCK reference node field to the parent node. Since this is a numeric arguement, you have to write a small amount of PHP while copies the node id (nid) to the view argument variable (Note: Arguments are really just parameterized Filters who's values can come from the current URL via string matching or from custom PHP as described here).

      - Admin/Views:
      - Create a Block-type view for your sub-view.
      - Add any fields or view type you would normally use.
      - Add an Argument of type Node Reference: <Parent Node Type>
      - In the Argument Handle Code paste in:
      ----------------
      if ($view->build_type == 'block' && arg(0) == 'node' && is_numeric(arg(1))) {
      $args[0] = arg(1);
      }
      return $args;
      ----------------
      - Save
      - Admin/Blocks:
      - Configure the block to display only on the parent node path (e.g., "companies/*" - assuming you're using pathauto).

      It's not that elegant - but it's a very minimal amount of additional PHP code.

    You must be Mike's friend before you can comment on this Fave.
    Send Mike a friend request or a personal message instead.

Related Faves from mike