Last active
May 4, 2018 19:43
-
-
Save init90/30fb4ef97c4aef0e251c1b1f0406f9af to your computer and use it in GitHub Desktop.
Drupal 7, field view alter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Implements hook_field_attach_view_alter(). | |
| */ | |
| function MODULE_field_attach_view_alter(&$output, $context) { | |
| foreach (element_children($output) as $field_name) { | |
| if ($field_name == 'field_cmrc_price_table') { | |
| // If min and max price have the same value, then display price without range. | |
| if (isset($output['field_cmrc_price_table'][0]['#markup']) && !empty($output['field_cmrc_price_table'][0]['#markup'])) { | |
| $separated_price = explode('–', $output['field_cmrc_price_table'][0]['#markup']); | |
| if (count($separated_price) == 2) { | |
| $min_price = gm_wizard_get_number_from_string($separated_price[0]); | |
| $max_price = gm_wizard_get_number_from_string($separated_price[1]); | |
| $prices_corrects = (is_numeric($min_price) && is_numeric($max_price)); | |
| $prices_are_equal = ($min_price == $max_price); | |
| if ($prices_corrects && $prices_are_equal) { | |
| $output['field_cmrc_price_table'][0]['#markup'] = trim($separated_price[0]); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * Get number from string. | |
| * | |
| * @param string $string | |
| * @return int|float|NULL $number | |
| */ | |
| function gm_wizard_get_number_from_string($string) { | |
| $number = NULL; | |
| preg_match_all('#\d+\.*\d*#', $string, $match); | |
| if (isset($match[0][0]) && is_numeric($match[0][0])) { | |
| $number = $match[0][0]; | |
| } | |
| return $number; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment