You are here

public static function WebformOptions::getElementOptions in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Entity/WebformOptions.php \Drupal\webform\Entity\WebformOptions::getElementOptions()

Get webform element options.

Parameters

array $element: A webform element.

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

Return value

array An associative array of options.

Overrides WebformOptionsInterface::getElementOptions

8 calls to WebformOptions::getElementOptions()
WebformElementBase::initialize in src/Plugin/WebformElementBase.php
Initialize an element to be displayed, rendered, or exported.
WebformElementController::autocomplete in src/Controller/WebformElementController.php
Returns response for the element autocomplete route.
WebformLikert::initialize in src/Plugin/WebformElement/WebformLikert.php
Initialize an element to be displayed, rendered, or exported.
WebformMapping::initialize in src/Plugin/WebformElement/WebformMapping.php
Initialize an element to be displayed, rendered, or exported.
WebformOptionsCustom::setOptions in modules/webform_options_custom/src/Plugin/WebformElement/WebformOptionsCustom.php
Get element custom options.

... See full list

File

src/Entity/WebformOptions.php, line 207

Class

WebformOptions
Defines the webform options entity.

Namespace

Drupal\webform\Entity

Code

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

  // If element already has #options array just call alter hook with
  // a NULL id.
  if (is_array($element[$property_name])) {
    $options = $element[$property_name];
    \Drupal::moduleHandler()
      ->alter('webform_options', $options, $element);
    return $options;
  }

  // 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 ($webform_options = WebformOptions::load($id)) {
    $options = $webform_options
      ->getOptions() ?: [];
  }
  else {
    $options = [];
  }

  // Alter options using hook_webform_options_alter()
  // and/or hook_webform_options_WEBFORM_OPTIONS_ID_alter() hook.
  // @see webform.api.php
  \Drupal::moduleHandler()
    ->alter('webform_options_' . $id, $options, $element);
  \Drupal::moduleHandler()
    ->alter('webform_options', $options, $element, $id);

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