1

I have two content types that are being linked via entity references.

  1. Area of Expertise [Existing URL: /expertise/specific-area-here]
  2. Employee [Desired URL: /expertise/specific-area-here/staff/specific-employee-here]

Employee has an entity reference field that contains all of the areas of expertise and only one value is allowed to be selected.

For example: I have an area of expertise called "Pathology" and an employee named "John Doe". In John Doe, I select that the area of expertise is "Pathology".

When I'm on the area of expertise page for a particular area of expertise, I am showing all of the employees that specialize in that area of expertise in the sidebar second region. To do that I'm using a view that generates a block. Within the view I have a contextual filter using the following chunk of PHP code to provide the default filter value:

$filterValue = -1;

$node = menu_get_object();

if(!empty($node))
{
    switch($node->type)
    {
        case 'area_of_expertise':
            $filterValue = $node->nid;
            break;
    }
}

return $filterValue;

All of that is working properly.

What I want to do now is have a staff page that shows all of the staff associated with a particular area of expertise. I want the URL to be /expertise/specific-area-here/staff.

For example, if I want to see all of the staff that are associated with the area of expertise called "Pathology" (located at /expertise/pathology) then I would want the staff page to be available at /expertise/pathology/staff.

The issue I'm having is figuring out how to set up the contextual filter such that the "/expertise/pathology" piece of the URL of /expertise/pathology/staff can be processed so that Drupal knows which area of expertise to use to filter.

Also, the breadcrumbs should work as follows:

If I'm on the Pathology area of expertise page, the breadcrumbs should show:

Home > Pathology

If I'm on the Pathology staff page, the breadcrumbs should show:

Home > Pathology > Staff

If I've drilled down into John Doe's page, the breadcrumbs should show:

Home > Pathology > Staff > John Doe

How should I setup the contextual filter to allow this behavior and how should I configure the "Path" under "Page Settings" in the view?

1

After much trial and error, I found a solution that seems to work for me.

The first thing I had to do was switch from using a "provide default filter value" to specifying custom validation criteria via PHP:

$normalPath = drupal_get_normal_path('expertise/' . $argument);
$urlPieces = explode('/', $normalPath);

if(!isset($urlPieces[1]))
{
    return false;
}

$nodeId = $urlPieces[1];
$node = node_load($nodeId);

if(!empty($node))
{
    switch($node->type)
    {
        case 'area_of_expertise':
            $handler->argument = $node->nid;
            return true;
    }
}

return false;

Thanks to Cameron for the bits about $argument and $handler->argument.

Once I figured out how to use argument to generate the URL which then allowed me to look-up the node ID, I was able to reuse most of the code that I had before in the "Provide Default Value" section.

The next issue was the breadcrumbs. Before I did anything extra, the breadcrumbs were showing as Home > > Staff. I ended up having to create a new menu item called Staff in the main menu that was a child of Pathology. So then the breadcrumbs showed as Home > Pathology > Staff > Staff which is still not what I wanted.

I found out that views likes to add each contextual filter as a breadcrumb so that you can navigate up and get less and less granular. Normally this would be a nice feature, but I couldn't see any way to turn it off.

So I found a post that detailed how to remove the breadcrumbs which I tried to implement via hook_preprocess_page. This ended up not affecting the breadcrumbs at all because the views module was altering the breadcrumbs after that hook was being run.

Then I found another post that detailed how to affect the breadcrumbs after the view was ready to be rendered using hook_views_pre_render.

So I have the following code as my hook:

function MODULE_views_pre_render(&$vars)
{
    $page = menu_get_item();

    if(($page['page_callback'] == 'views_page') && ($page['page_arguments'][0] == 'staff_cross_reference'))
    {
        $breadcrumbs = drupal_get_breadcrumb();
        array_pop($breadcrumbs);
        drupal_set_breadcrumb($breadcrumbs);
    }
}

And now my breadcrumbs look like Home > Pathology > Staff which is exactly what I want!

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.