function redhen_fields_extract_allowed_labels in RedHen CRM 7
Parses a string of 'allowed labels' into an array.
Parameters
string $string: The list of allowed labels in string format descibed in redhen_fields_allowed_labels_string().
bool $generate_keys: Boolean value indicating whether to generate keys based on the position of the value if a key is not manually specified, and if the value cannot be used as a key.
Return value
array The array of extracted key/value pairs, or NULL if the string is invalid.
See also
redhen_fields_allowed_labels_string()
1 call to redhen_fields_extract_allowed_labels()
- redhen_fields_allowed_labels_setting_validate in modules/
redhen_fields/ redhen_fields.module - Element validate callback; check that the entered values are valid.
File
- modules/
redhen_fields/ redhen_fields.module, line 196 - Defines email, phone and address field types for RedHen CRM.
Code
function redhen_fields_extract_allowed_labels($string, $generate_keys) {
$values = array();
$list = explode("\n", $string);
$list = array_map('trim', $list);
$list = array_filter($list, 'strlen');
$generated_keys = $explicit_keys = FALSE;
foreach ($list as $position => $text) {
$value = $key = FALSE;
// Check for an explicit key.
$matches = array();
if (preg_match('/(.*)\\|(.*)/', $text, $matches)) {
$key = $matches[1];
$value = $matches[2];
$explicit_keys = TRUE;
}
elseif (is_numeric($text) && (double) $text == intval($text)) {
$key = $value = $text;
$explicit_keys = TRUE;
}
elseif ($generate_keys) {
$key = (string) $position;
$value = $text;
$generated_keys = TRUE;
}
else {
return;
}
$values[$key] = $value;
}
// We generate keys only if the list contains no explicit key at all.
if ($explicit_keys && $generated_keys) {
return;
}
return $values;
}