You are here

public function ConditionalFieldsFormHelper::elementAddProperty in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 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

array $element: The form element that we're adding a property to.

string $property: The property to add.

mixed $value: The value for the property to add.

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.

Return value

array The modified element.

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

File

src/ConditionalFieldsFormHelper.php, line 821

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

public function elementAddProperty(array $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 $element;
  }
  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;
  }
  return $element;
}