You are here

public static function YamlFormOptions::getElementOptions in YAML Form 8

Get form element options.

Parameters

array $element: A form element.

string $property_name: The element property containing the options. Defaults to #options, for yamlform_likert elements it is #answers.

Return value

array An associative array of options.

Overrides YamlFormOptionsInterface::getElementOptions

8 calls to YamlFormOptions::getElementOptions()
YamlFormCompositeBase::processYamlFormComposite in src/Element/YamlFormCompositeBase.php
Processes a composite form element.
YamlFormElementBase::initialize in src/YamlFormElementBase.php
Initialize an element to be displayed, rendered, or exported.
YamlFormElementController::autocomplete in src/Controller/YamlFormElementController.php
Returns response for the element autocomplete route.
YamlFormLikert::initialize in src/Plugin/YamlFormElement/YamlFormLikert.php
Initialize an element to be displayed, rendered, or exported.
YamlFormOptionsController::autocomplete in src/Controller/YamlFormOptionsController.php
Returns response for the element autocompletion.

... See full list

File

src/Entity/YamlFormOptions.php, line 140

Class

YamlFormOptions
Defines the form options entity.

Namespace

Drupal\yamlform\Entity

Code

public static function getElementOptions(array $element, $property_name = '#options') {

  // If element already has #options return them.
  if (is_array($element[$property_name])) {
    return $element[$property_name];
  }

  // Return empty options if element does not define an options id.
  if (empty($element[$property_name]) || !is_string($element[$property_name])) {
    return [];
  }

  // If options have been set return them.
  // This allows dynamic options to be overridden.
  $id = $element[$property_name];
  if ($yamlform_options = YamlFormOptions::load($id)) {
    $options = $yamlform_options
      ->getOptions();
    if ($options) {
      return $options;
    }
  }

  // Get options using alter hook.
  $options = [];
  \Drupal::moduleHandler()
    ->alter('yamlform_options_' . $id, $options, $element);

  // Log empty options.
  if (empty($options)) {
    \Drupal::logger('yamlform')
      ->notice('Options %id do not exist.', [
      '%id' => $id,
    ]);
  }
  return $options;
}