Search code examples
phpdrupaldrupal-7drupal-views

How do I get data from a content type field into a view?


I have a client who wants to be able to put a custom header on certain views. However, I don't want to give him access to edit the view header, so I've put a field in a content type that is being used for the view with the machine name field_related_content_title.

The machine name for the content type used is landing_page, and the view's machine name is related_content.

I'm not quite sure how to make this work. I have the basic idea, that I would use a hook in template.php (but not at all sure which one to use), and I'm sure my logic is faulty.

Any tips would be appreciated. Thanks!


Solution

  • Configure the view in order to have a header (Custom text, por example). Then, in your module you can use the hook views_pre_render:

    function mymodule_views_pre_render(&$view) {
        $view->header['area_text_custom']->options['content'] = $view->result[0]->field_related_content_title[0]['rendered'];
    }
    

    This will put the value of the field_related_content_title field (for the first views result) in the header of the view.