You are here

protected function WebformEntityHandler::getEntityFieldsForm in Webform Entity Handler 8

Same name and namespace in other branches
  1. 2.x src/Plugin/WebformHandler/WebformEntityHandler.php \Drupal\webform_entity_handler\Plugin\WebformHandler\WebformEntityHandler::getEntityFieldsForm()

Compose the form with the entity type fields.

Parameters

string $entity_type_bundle: The entity type with its bundle.

Return value

array The composed form with the entity type fields.

1 call to WebformEntityHandler::getEntityFieldsForm()
WebformEntityHandler::buildConfigurationForm in src/Plugin/WebformHandler/WebformEntityHandler.php
Form constructor.

File

src/Plugin/WebformHandler/WebformEntityHandler.php, line 534

Class

WebformEntityHandler
Create or update an entity with a webform submission values.

Namespace

Drupal\webform_entity_handler\Plugin\WebformHandler

Code

protected function getEntityFieldsForm($entity_type_bundle) {
  $form = [];
  [
    $type,
    $bundle,
  ] = explode(':', $entity_type_bundle);

  /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $properties */
  $fields = $this->entityFieldManager
    ->getFieldDefinitions($type, $bundle);
  foreach ($fields as $field_name => $field) {
    $base_field = BaseFieldDefinition::create($field
      ->getType());
    $field_properties = method_exists($field, 'getPropertyDefinitions') ? $field
      ->getPropertyDefinitions() : $base_field
      ->getPropertyDefinitions();
    if (empty($field_properties)) {
      $field_properties = $base_field
        ->getPropertyDefinitions();
    }
    $field_schema = method_exists($field, 'getSchema') ? $field
      ->getSchema() : $base_field
      ->getSchema();

    // Use only properties with schema.
    if (!empty($field_schema['columns'])) {
      $field_properties = array_intersect_key($field_properties, $field_schema['columns']);
    }
    if (!empty($field_properties)) {
      $form[$field_name] = [
        '#type' => 'details',
        '#title' => $this
          ->t('@label (Property: @name - Type: @type)', [
          '@label' => $field
            ->getLabel(),
          '@name' => $field_name,
          '@type' => $field
            ->getType(),
        ]),
        '#description' => $field
          ->getDescription(),
        '#open' => FALSE,
        '#required' => $field
          ->isRequired(),
      ];
      foreach ($field_properties as $property_name => $property) {
        $form[$field_name][$property_name] = [
          '#type' => 'webform_select_other',
          '#title' => $this
            ->t('Column: @name - Type: @type', [
            '@name' => $property
              ->getLabel(),
            '@type' => $property
              ->getDataType(),
          ]),
          '#description' => $property
            ->getDescription(),
          '#options' => [
            '_null_' => $this
              ->t('Null'),
          ] + $this
            ->getElements() + [
            WebformSelectOther::OTHER_OPTION => $this
              ->t('Custom value…'),
          ],
          '#default_value' => $this->configuration['entity_values'][$field_name][$property_name] ?? NULL,
          '#empty_value' => NULL,
          '#empty_option' => $this
            ->t('- Select -'),
          '#parents' => [
            'settings',
            'entity_values',
            $field_name,
            $property_name,
          ],
          // @todo Use the property type.
          '#other__type' => 'textfield',
        ];
      }
      $form[$field_name]['webform_entity_handler_append'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('If checked, the value will be appended rather than overridden. Only apply updating an entity.'),
        '#default_value' => $this->configuration['entity_values'][$field_name]['webform_entity_handler_append'] ?? NULL,
        '#parents' => [
          'settings',
          'entity_values',
          $field_name,
          'webform_entity_handler_append',
        ],
        '#access' => $field
          ->getFieldStorageDefinition()
          ->getCardinality() != 1,
      ];
    }
  }

  // Remove the entity ID and bundle, they have theirs own settings.
  try {
    $entity_id_key = $this->entityTypeManager
      ->getDefinition($type)
      ->getKey('id');
    unset($form[$entity_id_key]);
    if ($this->entityTypeManager
      ->getDefinition($type)
      ->hasKey('bundle')) {
      unset($form[$this->entityTypeManager
        ->getDefinition($type)
        ->getKey('bundle')]);
    }
  } catch (\Exception $exception) {

    // Nothing to do.
  }
  return $form;
}