You are here

public function ConditionalFieldsFormHelper::elementAddProperty in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/ConditionalFieldsFormHelper.php \Drupal\conditional_fields\ConditionalFieldsFormHelper::elementAddProperty()

Helper function to add a property/value pair to a render array.

Safely without overriding any pre-existing value.

Parameters

string $position: Use 'append' if $value should be inserted at the end of the $element[$property] array, any other value to insert it at the beginning.

1 call to ConditionalFieldsFormHelper::elementAddProperty()
ConditionalFieldsFormHelper::processDependeeFields in src/ConditionalFieldsFormHelper.php
Determine and register dependee field effects.

File

src/ConditionalFieldsFormHelper.php, line 728

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

public function elementAddProperty(&$element, $property, $value, $position = 'prepend') {

  // Avoid overriding default element properties that might not yet be set.
  if (!isset($element[$property])) {
    $element[$property] = isset($element['#type']) ? $this->elementInfo
      ->getInfoProperty($element['#type'], $property, []) : [];
  }
  if (is_array($value)) {

    // A method callback, wrap it around.
    $value = [
      $value,
    ];
  }
  if (in_array($value, $element[$property])) {
    return;
  }
  switch ($position) {
    case 'append':
      $element[$property] = array_merge($element[$property], (array) $value);
      break;
    case 'prepend':
    default:
      $element[$property] = array_merge((array) $value, $element[$property]);
      break;
  }
}