You are here

public function FlexiformFormEntityTypedDataReferencedEntity::configurationForm in Flexiform 8

Prepare a configuration form.

Overrides FlexiformFormEntityBase::configurationForm

File

src/Plugin/FlexiformFormEntity/FlexiformFormEntityTypedDataReferencedEntity.php, line 152

Class

FlexiformFormEntityTypedDataReferencedEntity
Form Entity plugin.

Namespace

Drupal\flexiform\Plugin\FlexiformFormEntity

Code

public function configurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::configurationForm($form, $form_state);
  $form['create'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Create New Entity'),
    '#description' => $this
      ->t('If the property is empty, and new entity will be created.'),
    '#default_value' => !empty($this->configuration['create']),
  ];

  // Set up ownership, if relevant.
  if ($this->entityTypeManager
    ->getDefinition($this
    ->getEntityType())
    ->entityClassImplements(EntityOwnerInterface::class)) {
    $form['owner_context'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Set owner to'),
      '#description' => $this
        ->t('This will not override an existing owner.'),
      '#default_value' => $this->configuration['owner_context'] ?? '_none',
      '#options' => [
        '_current_user' => $this
          ->t('Current user'),
      ],
      '#empty_option' => $this
        ->t('Do not set owner'),
      '#empty_value' => '_none',
      '#states' => [
        'visible' => [
          ':input[name="configuration[save_on_submit]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="configuration[create]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $context_definition = new ContextDefinition('entity:user', NULL, TRUE, FALSE);
    $matching_contexts = $this
      ->contextHandler()
      ->getMatchingContexts($this->formEntityManager
      ->getContexts(), $context_definition);
    foreach ($matching_contexts as $context) {
      $form['owner_context']['#options'][$context
        ->getEntityNamespace()] = $context
        ->getContextDefinition()
        ->getLabel();
    }
  }
  return $form;
}