You are here

protected function YamlForm::initElements in YAML Form 8

Initialize parse form elements.

7 calls to YamlForm::initElements()
YamlForm::getElementsDecoded in src/Entity/YamlForm.php
Get form elements decoded as an associative array.
YamlForm::getElementsDecodedAndFlattened in src/Entity/YamlForm.php
Get form raw elements decoded and flattened into an associative array.
YamlForm::getElementsFlattenedAndHasValue in src/Entity/YamlForm.php
Get form flattened list of elements.
YamlForm::getElementsInitialized in src/Entity/YamlForm.php
Get form elements initialized as an associative array.
YamlForm::getElementsInitializedAndFlattened in src/Entity/YamlForm.php
Get form elements initialized and flattened into an associative array.

... See full list

File

src/Entity/YamlForm.php, line 755

Class

YamlForm
Defines the form entity.

Namespace

Drupal\yamlform\Entity

Code

protected function initElements() {
  if (isset($this->elementsInitialized)) {
    return;
  }
  $this->elementsDecodedAndFlattened = [];
  $this->elementsInitializedAndFlattened = [];
  $this->elementsFlattenedAndHasValue = [];
  $this->elementsTranslations = [];
  try {

    /** @var \Drupal\yamlform\YamlFormTranslationManagerInterface $translation_manager */
    $translation_manager = \Drupal::service('yamlform.translation_manager');

    /** @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */
    $language_manager = \Drupal::service('language_manager');

    // If current form is translated, load the base (default) form and apply
    // the translation to the elements.
    if ($this->langcode != $language_manager
      ->getCurrentLanguage()
      ->getId()) {
      $default_langcode = $language_manager
        ->getDefaultLanguage()
        ->getId();
      $elements = $translation_manager
        ->getConfigElements($this, $default_langcode);
      $this->elementsTranslations = Yaml::decode($this->elements);
    }
    else {
      $elements = Yaml::decode($this->elements);
    }

    // Since YAML supports simple values.
    $elements = is_array($elements) ? $elements : [];
    $this->elementsDecoded = $elements;
  } catch (\Exception $exception) {
    $link = $this
      ->link(t('Edit'), 'edit-form');
    \Drupal::logger('yamlform')
      ->notice('%title elements are not valid. @message', [
      '%title' => $this
        ->label(),
      '@message' => $exception
        ->getMessage(),
      'link' => $link,
    ]);
    $elements = FALSE;
  }
  if ($elements !== FALSE) {
    $this
      ->initElementsRecursive($elements);
    $this
      ->invokeHandlers('alterElements', $elements, $this);
  }
  $this->elementsInitialized = $elements;
}