0

I have content type which has field_pdf.
I have made view to show content and there is a field which has set: Output field as a link. In link path settings I have two tokens: [field_pdf][path]. Idea is to link field either straight to PDF (if there is PDF file in node) or to content node.

How to get view to use only [field_pdf] when this field_pdf has content and not use [path] at all?

Now I'm getting double url for example: http://url.com/pdf-file.pdfhttp://url.com/pdf

1 Answer 1

1

Modules You Need:

  1. Views
  2. Views PHP

Assuming your content type is having these fields:

fields

The VIEWS Code:

Simply go to admin/structure/views/import and then Import the below mentioned code:

$view = new view();
$view->name = 'pdf';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'PDF';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'PDF';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: PDF */
$handler->display->display_options['fields']['field_pdf']['id'] = 'field_pdf';
$handler->display->display_options['fields']['field_pdf']['table'] = 'field_data_field_pdf';
$handler->display->display_options['fields']['field_pdf']['field'] = 'field_pdf';
$handler->display->display_options['fields']['field_pdf']['label'] = '';
$handler->display->display_options['fields']['field_pdf']['exclude'] = TRUE;
$handler->display->display_options['fields']['field_pdf']['alter']['make_link'] = TRUE;
$handler->display->display_options['fields']['field_pdf']['alter']['path'] = '[field_pdf][path]';
$handler->display->display_options['fields']['field_pdf']['element_label_colon'] = FALSE;
/* Field: Global: PHP */
$handler->display->display_options['fields']['php']['id'] = 'php';
$handler->display->display_options['fields']['php']['table'] = 'views';
$handler->display->display_options['fields']['php']['field'] = 'php';
$handler->display->display_options['fields']['php']['label'] = '';
$handler->display->display_options['fields']['php']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['php']['use_php_setup'] = 0;
$handler->display->display_options['fields']['php']['php_output'] = '<?php
if (isset($data->field_field_pdf[0][\'rendered\'][\'#markup\'])) {
    $pdf_url = $data->field_field_pdf[0][\'rendered\'][\'#markup\'];
    print $pdf_url;
} else {
    global $base_url;
    $node_url = $base_url . \'/node/\' . $data->nid;
    print $node_url;
}
?>';
$handler->display->display_options['fields']['php']['use_php_click_sortable'] = '0';
$handler->display->display_options['fields']['php']['php_click_sortable'] = '';
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'pdf' => 'pdf',
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'pdf';

I'm sure you might be having some dummy content for that content-type, if not then do create couple of ones. Some with values in field_php and some with not!

and Voila! Here is your required output:

preview

Updated Code:

$view = new view();
$view->name = 'pdf';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'PDF';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'PDF';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: PDF */
$handler->display->display_options['fields']['field_pdf']['id'] = 'field_pdf';
$handler->display->display_options['fields']['field_pdf']['table'] = 'field_data_field_pdf';
$handler->display->display_options['fields']['field_pdf']['field'] = 'field_pdf';
$handler->display->display_options['fields']['field_pdf']['label'] = '';
$handler->display->display_options['fields']['field_pdf']['exclude'] = TRUE;
$handler->display->display_options['fields']['field_pdf']['alter']['make_link'] = TRUE;
$handler->display->display_options['fields']['field_pdf']['alter']['path'] = '[field_pdf][path]';
$handler->display->display_options['fields']['field_pdf']['element_label_colon'] = FALSE;
/* Field: Global: PHP */
$handler->display->display_options['fields']['php']['id'] = 'php';
$handler->display->display_options['fields']['php']['table'] = 'views';
$handler->display->display_options['fields']['php']['field'] = 'php';
$handler->display->display_options['fields']['php']['label'] = '';
$handler->display->display_options['fields']['php']['alter']['make_link'] = TRUE;
$handler->display->display_options['fields']['php']['alter']['path'] = '[php]';
$handler->display->display_options['fields']['php']['alter']['absolute'] = TRUE;
$handler->display->display_options['fields']['php']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['php']['use_php_setup'] = 0;
$handler->display->display_options['fields']['php']['php_output'] = '<?php
if (isset($data->field_field_pdf[0][\'rendered\'][\'#markup\'])) {
    $pdf_url = $data->field_field_pdf[0][\'rendered\'][\'#markup\'];
    print $pdf_url;
} else {
    global $base_url;
    $node_url = $base_url . \'/node/\' . $data->nid;
    print $node_url;
}
?>';
$handler->display->display_options['fields']['php']['use_php_click_sortable'] = '0';
$handler->display->display_options['fields']['php']['php_click_sortable'] = '';
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'pdf' => 'pdf',
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'pdf';

Let me know if you need anything else from this.

Cheers!

5
  • I added content type PDF and field_pdf. Then I added content with pdf file and without pdf file. Imported new views with the views code above. But views shows only normal paths, not path to the file.
    – hene
    Jul 22, 2014 at 7:36
  • @hene, you mean to say you want clickable links? Jul 22, 2014 at 7:38
  • Yes. Im building views which shows multiple content from multiple content types. And some of the content has link to the node itself but some content has PDF-file and in those link should be straight to PDF-file instead of node.
    – hene
    Jul 22, 2014 at 7:40
  • @hene, I've updated the post with the new code, please try re-importing it and do check it. I'm sure it will work as per your requirement. Let me know if there is any issue. Jul 22, 2014 at 7:41
  • Ah, now it works. I imported updated views code AND changed Field Content: PHP Formatter from Generic file to URL to file.
    – hene
    Jul 22, 2014 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.