You are here

function _field_label_generate_styles in Field Label 8

Create an array of validated key/value pairs of class selector and labels.

Return value

array An array with valid values, or empty values if selector is not valid

2 calls to _field_label_generate_styles()
field_label_field_formatter_third_party_settings_form in ./field_label.module
Implements hook_field_formatter_third_party_settings_form().
_field_label_is_valid_style in ./field_label.module
Ensure a style selector is still available in the configured class list.

File

./field_label.module, line 270
Provides custom settings to format field labels.

Code

function _field_label_generate_styles() {
  $class_list = \Drupal::config('field_label.settings')
    ->get('class_list');
  $styles = [];

  // Return early if class list is emptty.
  $class_list = trim($class_list);
  if (empty($class_list)) {
    return $styles;
  }

  // Handle newlines.
  $class_list = str_replace([
    "\r\n",
    "\r",
  ], "\n", $class_list);
  foreach (explode("\n", $class_list) as $style) {

    // Ignore empty lines in between non-empty lines.
    if (empty($style)) {
      continue;
    }

    // Validate syntax: [.class...]|label pattern expected.
    if (!preg_match('@^(\\.[a-zA-Z0-9_-]+)+\\| *.+ *$@', $style)) {

      // Skip invalid patterns.
      continue;
    }
    list($selector, $label) = explode('|', $style);
    $styles[$selector] = $label;
  }
  return $styles;
}