GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
post_image.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Display the fileupload field type
4  *
5  * @package GravityView
6  * @subpackage GravityView/templates/fields
7  */
8 
10 
11 extract( $gravityview_view->getCurrentField() );
12 
13 // Tell the renderer not to wrap this field in an anchor tag.
14 $gravityview_view->setCurrentFieldSetting('show_as_link', false);
15 
16 /**
17  * Parse the stored value of the post image
18  *
19  * The image is stored with `|:|` dividing the fields. Break it into its parts and see what's set.
20  *
21  * @see GFCommon::get_lead_field_display()
22  * @var array
23  */
24 $ary = explode("|:|", $value);
25 $url = count($ary) > 0 ? $ary[0] : "";
26 $title = count($ary) > 1 ? $ary[1] : "";
27 $caption = count($ary) > 2 ? $ary[2] : "";
28 $description = count($ary) > 3 ? $ary[3] : "";
29 
30 $link_atts = array();
31 
32 /**
33  * @since 1.5.4
34  *
35  * $field['postFeaturedImage'] - holds if the Post Image field is set as post featured image
36  * $field_settings['dynamic_data'] - whether the field content should be fetched from the Post (dynamic data) or from the GF entry
37  *
38  * Dynamic data (get post featured image instead of GF entry field)
39  */
40 if( !empty( $field['postFeaturedImage'] ) && !empty( $field_settings['dynamic_data'] ) && !empty( $entry['post_id'] ) && has_post_thumbnail( $entry['post_id'] ) ) {
41 
42  /**
43  * Modify what size is fetched for the post's Featured Image
44  * @param string $size The size to be fetched using `wp_get_attachment_image_src()` (default: 'large')
45  * @param array $entry Gravity Forms entry array
46  */
47  $image_size = apply_filters( 'gravityview/fields/post_image/size', 'large', $entry );
48  $image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $entry['post_id'] ), $image_size );
49 
50  if( empty( $image_url[0] ) ) {
51  do_action('gravityview_log_debug', 'Dynamic featured image for post #'.$entry['post_id'].' doesnt exist (size: '.$image_size.').' );
52  } else {
53  $url = $image_url[0];
54  }
55 }
56 
57 ##
58 ## Get the link URL
59 ##
60 
61 // Link to the post created by the entry
62 if( !empty( $field_settings['link_to_post'] ) ) {
63  $href = get_permalink( $entry['post_id'] );
64 }
65 // Link to the single entry
66 else if ( !empty( $field_settings['show_as_link'] ) ) {
68 }
69 // Link to the file itself
70 else {
71 
72  $href = $url;
73 
74  // Only show the lightbox if linking to the file itself
75  if( $gravityview_view->getAtts('lightbox') ) {
76  $link_atts['class'] = apply_filters( 'gravityview_lightbox_script', 'thickbox' );
77  }
78 
79 }
80 
81 
82 // Set the attributes for the link
83 $link_atts['href'] = $href;
84 
85 // Add the title as the link title, if exists. This will appear as caption in the lightbox.
86 $link_atts['title'] = $title;
87 
88 
89 ##
90 ## Get the image
91 ##
92 $image_atts = array(
93  'src' => $url,
94  'alt' => ( !empty( $caption ) ? $caption : $title ),
95  'validate_src' => false, // Already validated by GF
96 );
97 
99 
100 
101 /**
102  * @filter `gravityview_post_image_meta` Modify the values used for the image meta.
103  * @see https://gravityview.co/support/documentation/201606759 Read more about the filter
104  * @var array $image_meta Associative array with `title`, `caption`, and `description` keys, each an array with `label`, `value`, `tag_label` and `tag_value` keys
105  */
106 $image_meta = apply_filters('gravityview_post_image_meta', array(
107  'title' => array(
108  'label' => esc_attr_x( 'Title:', 'Post Image field title heading', 'gk-gravityview'),
109  'value' => $title,
110  'tag_label' => 'span',
111  'tag_value' => 'div'
112  ),
113  'caption' => array(
114  'label' => esc_attr_x( 'Caption:', 'Post Image field caption heading', 'gk-gravityview'),
115  'value' => $caption,
116  'tag_label' => 'span',
117  'tag_value' => GFFormsModel::is_html5_enabled() ? 'figcaption' : 'div',
118  ),
119  'description' => array(
120  'label' => esc_attr_x( 'Description:', 'Post Image field description heading', 'gk-gravityview'),
121  'value' => $description,
122  'tag_label' => 'span',
123  'tag_value' => 'div'
124  ),
125 ));
126 
127 // If HTML5 output is enabled, support the `figure` and `figcaption` tags
128 $wrappertag = GFFormsModel::is_html5_enabled() ? 'figure' : 'div';
129 
130 /**
131  * @filter `gravityview_post_image_meta_show_labels` Whether to show labels for the image meta.
132  * @see https://gravityview.co/support/documentation/201606759 Read more about the filter
133  * @var boolean $showlabels True: Show labels; False: hide labels
134  */
135 $showlabels = apply_filters( 'gravityview_post_image_meta_show_labels', true );
136 
137 // Wrapper tag
138 $output = '<'.$wrappertag.' class="gv-image">';
139 
140 // Image with link tag
142 
143 foreach ( (array)$image_meta as $key => $meta ) {
144 
145  if( !empty( $meta['value'] ) ) {
146 
147  $output .= '<div class="gv-image-'.esc_attr( $key ).'">';
148 
149  // Display the label if the label's not empty
150  if( !empty( $showlabels ) && !empty( $meta['label'] ) ) {
151  $output .= '<'.esc_attr( $meta['tag_label'] ).' class="gv-image-label">';
152  $output .= esc_html( $meta['label'] );
153  $output .= '</'.esc_attr( $meta['tag_label'] ).'> ';
154  }
155 
156  // Display the value
157  $output .= '<'.esc_attr( $meta['tag_value'] ).' class="gv-image-value">';
158  $output .= esc_html( $meta['value'] );
159  $output .= '</'.esc_attr( $meta['tag_value'] ).'>';
160 
161  $output .= '</div>';
162  }
163 
164 }
165 
166 $output .= '</'.$wrappertag.'>';
167 
168 echo $output;
$url
Definition: post_image.php:25
$image
Definition: post_image.php:98
$caption
Definition: post_image.php:27
Generic class for generating image tag.
static getInstance( $passed_post=NULL)
$image_meta
Definition: post_image.php:106
$ary
Definition: post_image.php:24
$showlabels
Definition: post_image.php:135
$field_settings['content']
Definition: custom.php:27
gravityview_get_link( $href='', $anchor_text='', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
$gravityview_view
Definition: post_image.php:9
$image_atts
Definition: post_image.php:92
$link_atts
Definition: post_image.php:30
$output
Definition: post_image.php:138
gv_entry_link( $entry, $post_id=NULL)
Definition: class-api.php:938
$wrappertag
Definition: post_image.php:128
$entry
Definition: notes.php:27
$title
Definition: post_image.php:26
$description
Definition: post_image.php:28