/** * Given an entry and a form field id, calculate the entry value for that field. * * @access public * @param array $entry * @param integer $field * @return null|string */ public static function field_value($entry, $field_settings, $format = 'html') { if (empty($entry['form_id']) || empty($field_settings['id'])) { return NULL; } $gravityview_view = GravityView_View::getInstance(); if (class_exists('GFCache')) { /** * Gravity Forms' GFCache function was thrashing the database, causing double the amount of time for the field_value() method to run. * * The reason is that the cache was checking against a field value stored in a transient every time `GFFormsModel::get_lead_field_value()` is called. * * What we're doing here is telling the GFCache that it's already checked the transient and the value is false, forcing it to just use the non-cached data, which is actually faster. * * @hack * @since 1.3 * @param string $cache_key Field Value transient key used by Gravity Forms * @param mixed false Setting the value of the cache to false so that it's not used by Gravity Forms' GFFormsModel::get_lead_field_value() method * @param boolean false Tell Gravity Forms not to store this as a transient * @param int 0 Time to store the value. 0 is maximum amount of time possible. */ GFCache::set("GFFormsModel::get_lead_field_value_" . $entry["id"] . "_" . $field_settings["id"], false, false, 0); } $field_id = $field_settings['id']; $form = $gravityview_view->getForm(); $field = gravityview_get_field($form, $field_id); $field_type = RGFormsModel::get_input_type($field); if ($field_type) { $field_type = $field['type']; $value = RGFormsModel::get_lead_field_value($entry, $field); } else { // For non-integer field types (`id`, `date_created`, etc.) $field_type = $field_id; $field['type'] = $field_id; $value = isset($entry[$field_type]) ? $entry[$field_type] : NULL; } // Prevent any PHP warnings that may be generated ob_start(); $display_value = GFCommon::get_lead_field_display($field, $value, $entry["currency"], false, $format); if ($errors = ob_get_clean()) { do_action('gravityview_log_error', 'GravityView_API[field_value] Errors when calling GFCommon::get_lead_field_display()', $errors); } $display_value = apply_filters("gform_entry_field_value", $display_value, $field, $entry, $form); // prevent the use of merge_tags for non-admin fields if (!empty($field['adminOnly'])) { $display_value = self::replace_variables($display_value, $form, $entry); } // Check whether the field exists in /includes/fields/{$field_type}.php // This can be overridden by user template files. $field_exists = $gravityview_view->locate_template("fields/{$field_type}.php"); // Set the field data to be available in the templates $gravityview_view->setCurrentField(array('form' => $form, 'field_id' => $field_id, 'field' => $field, 'field_settings' => $field_settings, 'value' => $value, 'display_value' => $display_value, 'format' => $format, 'entry' => $entry, 'field_type' => $field_type)); if ($field_exists) { do_action('gravityview_log_debug', sprintf('[field_value] Rendering %s', $field_exists)); ob_start(); load_template($field_exists, false); $output = ob_get_clean(); } else { // Backup; the field template doesn't exist. $output = $display_value; } $field_settings = $gravityview_view->getCurrentField('field_settings'); /** * Link to the single entry by wrapping the output in an anchor tag * * Fields can override this by modifying the field data variable inside the field. See /templates/fields/post_image.php for an example. * */ if (!empty($field_settings['show_as_link'])) { $output = self::entry_link_html($entry, $output, array(), $field_settings); } /** * @filter `gravityview_field_entry_value_{$field_type}` Modify the field value output for a field type. Example: `gravityview_field_entry_value_number` * @since 1.6 * @param string $output HTML value output * @param array $entry The GF entry array * @param array $field_settings Settings for the particular GV field */ $output = apply_filters('gravityview_field_entry_value_' . $field_type, $output, $entry, $field_settings, $gravityview_view->getCurrentField()); /** * @filter `gravityview_field_entry_value` Modify the field value output for all field types * @param string $output HTML value output * @param array $entry The GF entry array * @param array $field_settings Settings for the particular GV field * @param array $field_data {@since 1.6} */ $output = apply_filters('gravityview_field_entry_value', $output, $entry, $field_settings, $gravityview_view->getCurrentField()); return $output; }
/** * Prepare search fields to frontend render with other details (label, field type, searched values) * * @param array $field * @return array */ private function get_search_filter_details($field) { $gravityview_view = GravityView_View::getInstance(); $form = $gravityview_view->getForm(); // for advanced field ids (eg, first name / last name ) $name = 'filter_' . str_replace('.', '_', $field['field']); // get searched value from $_GET/$_POST (string or array) $value = $this->rgget_or_rgpost($name); // get form field details $form_field = gravityview_get_field($form, $field['field']); $filter = array('key' => $field['field'], 'name' => $name, 'label' => self::get_field_label($field, $form_field), 'input' => $field['input'], 'value' => $value, 'type' => $form_field['type']); // collect choices if ('post_category' === $form_field['type'] && !empty($form_field['displayAllCategories']) && empty($form_field['choices'])) { $filter['choices'] = gravityview_get_terms_choices(); } elseif (!empty($form_field['choices'])) { $filter['choices'] = $form_field['choices']; } if ('date_range' === $field['input'] && empty($value)) { $filter['value'] = array('start' => '', 'end' => ''); } return $filter; }
/** * Given an entry and a form field id, calculate the entry value for that field. * * @access public * @param array $entry * @param array $field * @return null|string */ public static function field_value($entry, $field_settings, $format = 'html') { if (empty($entry['form_id']) || empty($field_settings['id'])) { return NULL; } $gravityview_view = GravityView_View::getInstance(); $field_id = $field_settings['id']; $form = $gravityview_view->getForm(); $field = gravityview_get_field($form, $field_id); if ($field && is_numeric($field_id)) { // Used as file name of field template in GV. // Don't use RGFormsModel::get_input_type( $field ); we don't care if it's a radio input; we want to know it's a 'quiz' field $field_type = $field->type; $value = RGFormsModel::get_lead_field_value($entry, $field); } else { $field = GravityView_Fields::get_associated_field($field_id); $field_type = $field_id; // Used as file name of field template in GV } // If a Gravity Forms Field is found, get the field display if ($field) { // Prevent any PHP warnings that may be generated ob_start(); $display_value = GFCommon::get_lead_field_display($field, $value, $entry["currency"], false, $format); if ($errors = ob_get_clean()) { do_action('gravityview_log_error', 'GravityView_API[field_value] Errors when calling GFCommon::get_lead_field_display()', $errors); } $display_value = apply_filters("gform_entry_field_value", $display_value, $field, $entry, $form); // prevent the use of merge_tags for non-admin fields if (!empty($field->adminOnly)) { $display_value = self::replace_variables($display_value, $form, $entry); } } else { $value = $display_value = rgar($entry, $field_id); $display_value = $value; } // Check whether the field exists in /includes/fields/{$field_type}.php // This can be overridden by user template files. $field_path = $gravityview_view->locate_template("fields/{$field_type}.php"); // Set the field data to be available in the templates $gravityview_view->setCurrentField(array('form' => $form, 'field_id' => $field_id, 'field' => $field, 'field_settings' => $field_settings, 'value' => $value, 'display_value' => $display_value, 'format' => $format, 'entry' => $entry, 'field_type' => $field_type, 'field_path' => $field_path)); if (!empty($field_path)) { do_action('gravityview_log_debug', sprintf('[field_value] Rendering %s', $field_path)); ob_start(); load_template($field_path, false); $output = ob_get_clean(); } else { // Backup; the field template doesn't exist. $output = $display_value; } // Get the field settings again so that the field template can override the settings $field_settings = $gravityview_view->getCurrentField('field_settings'); /** * @filter `gravityview_field_entry_value_{$field_type}_pre_link` Modify the field value output for a field type before Show As Link setting is applied. Example: `gravityview_field_entry_value_number_pre_link` * @since 1.16 * @param string $output HTML value output * @param array $entry The GF entry array * @param array $field_settings Settings for the particular GV field * @param array $field Field array, as fetched from GravityView_View::getCurrentField() */ $output = apply_filters('gravityview_field_entry_value_' . $field_type . '_pre_link', $output, $entry, $field_settings, $gravityview_view->getCurrentField()); /** * Link to the single entry by wrapping the output in an anchor tag * * Fields can override this by modifying the field data variable inside the field. See /templates/fields/post_image.php for an example. * */ if (!empty($field_settings['show_as_link']) && !gv_empty($output, false, false)) { $link_atts = empty($field_settings['new_window']) ? array() : array('target' => '_blank'); $output = self::entry_link_html($entry, $output, $link_atts, $field_settings); } /** * @filter `gravityview_field_entry_value_{$field_type}` Modify the field value output for a field type. Example: `gravityview_field_entry_value_number` * @since 1.6 * @param string $output HTML value output * @param array $entry The GF entry array * @param array $field_settings Settings for the particular GV field * @param array $field Current field being displayed */ $output = apply_filters('gravityview_field_entry_value_' . $field_type, $output, $entry, $field_settings, $gravityview_view->getCurrentField()); /** * @filter `gravityview_field_entry_value` Modify the field value output for all field types * @param string $output HTML value output * @param array $entry The GF entry array * @param array $field_settings Settings for the particular GV field * @param array $field_data {@since 1.6} */ $output = apply_filters('gravityview_field_entry_value', $output, $entry, $field_settings, $gravityview_view->getCurrentField()); return $output; }
/** * Prepare search fields to frontend render with other details (label, field type, searched values) * * @param type $field * @return type */ private function get_search_filter_details($field) { $gravityview_view = GravityView_View::getInstance(); $form = $gravityview_view->getForm(); // for advanced field ids (eg, first name / last name ) $name = 'filter_' . str_replace('.', '_', $field['field']); // get searched value from $_GET (string or array) $value = rgget($name); // get form field details $form_field = gravityview_get_field($form, $field['field']); $filter = array('key' => $field['field'], 'name' => $name, 'label' => $this->get_field_label($field, $form_field), 'input' => $field['input'], 'value' => $value, 'type' => $form_field['type']); // assign the correct label in case it is a boolean field if ('checkbox' === $form_field['type'] && false !== strpos($filter['key'], '.') && !empty($form_field['inputs'])) { foreach ($form_field['inputs'] as $input) { if ($input['id'] == $filter['key']) { $filter['label'] = $input['label']; break; } } } // collect choices if ('post_category' === $form_field['type'] && !empty($form_field['displayAllCategories']) && empty($form_field['choices'])) { $filter['choices'] = self::get_post_categories_choices(); } elseif (!empty($form_field['choices'])) { $filter['choices'] = $form_field['choices']; } if ('date_range' === $field['input'] && empty($value)) { $filter['value'] = array('start' => '', 'end' => ''); } return $filter; }
/** * Check whether the `enableChoiceValue` flag is set for a GF field * * Gets the current form ID, gets the field at that ID, then checks for the enableChoiceValue value. * * @access protected * * @uses GFAPI::get_form * * @since 1.17 * * @return bool True: Enable Choice Value is active for field; False: not active, or form invalid, or form not found. */ protected function is_choice_value_enabled() { // If "Add Field" button is processing, get the Form ID $connected_form = rgpost('form_id'); // Otherwise, get the Form ID from the Post page if (empty($connected_form)) { $connected_form = gravityview_get_form_id(get_the_ID()); } if (empty($connected_form)) { do_action('gravityview_log_error', sprintf('%s: Form not found for form ID "%s"', __METHOD__, $connected_form)); return false; } $form = GFAPI::get_form($connected_form); if (!$form) { do_action('gravityview_log_error', sprintf('%s: Form not found for field ID of "%s", when checking for a form with ID of "%s"', __METHOD__, $this->_field_id, $connected_form)); return false; } $field = gravityview_get_field($form, $this->_field_id); return !empty($field->enableChoiceValue); }
/** * @group fieldoutput * @see gravityview_field_output * @covers ::gravityview_field_output() */ public function test_gravityview_field_output() { /** * * 'entry' => null, * 'field' => null, * 'form' => null, * 'hide_empty' => true, * 'markup' => '<div id="{{ field_id }}" class="{{ class }}">{{label}}{{value}}</div>', * 'label_markup' => '', * 'wpautop' => false, * 'zone_id' => null, */ $form = $this->factory->form->create_and_get(); $entry = $this->factory->entry->create_and_get(array('form_id' => $form['id'])); $markup = '<div id="{{ field_id }}" class="{{ class }}">{{label}}{{value}}</div>'; $markup_without_spaces = '<div id="{{field_id}}" class="{{class}}">{{label}}{{value}}</div>'; $markup_just_label = '{{label}}'; $markup_just_value = '{{value}}'; $args = array('entry' => $entry, 'form' => $form, 'hide_empty' => $this->atts['hide_empty']); return; foreach ($entry as $field_id => $raw_field_value) { if (!is_numeric($field_id)) { continue; } $field = gravityview_get_field($form, $field_id); $args['field'] = $field; $value = gv_value($entry, $args['field']); $args['markup'] = $markup; $output = gravityview_field_output($args); //$this->assertEquals( , $output ); // Test hide empty } // Test gravityview/field_output/args filter add_filter('gravityview/field_output/args', array($this, '_filter_test_gravityview_field_output_args'), 10, 2); remove_filter('gravityview/field_output/args', array($this, '_filter_test_gravityview_field_output_args')); }