You are here

public function BackgroundImageManager::alterEntityForm in Background Image 2.0.x

Same name and namespace in other branches
  1. 8 src/BackgroundImageManager.php \Drupal\background_image\BackgroundImageManager::alterEntityForm()
  2. 2.x src/BackgroundImageManager.php \Drupal\background_image\BackgroundImageManager::alterEntityForm()

Perform alterations before a form is rendered.

Parameters

$form: Nested array of form elements that comprise the form.

$form_state: The current state of the form. The arguments that \Drupal::formBuilder()->getForm() was originally called with are available in the array $form_state->getBuildInfo()['args'].

Overrides BackgroundImageManagerInterface::alterEntityForm

File

src/BackgroundImageManager.php, line 175

Class

BackgroundImageManager

Namespace

Drupal\background_image

Code

public function alterEntityForm(array &$form, FormStateInterface $form_state) {

  // Check if inline_entity_form exists.
  $inline_entity_form = $this->moduleHandler
    ->moduleExists('inline_entity_form');

  // Only alter forms that have a "inline_entity_form_entity" set.
  // @see \Drupal\background_image\BackgroundImageManager::prepareForm

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $form_state
    ->get('inline_entity_form_entity');
  if (!$inline_entity_form || !$entity) {
    return;
  }
  $group = $this
    ->getEntityConfig($entity, 'group');
  $require = $this
    ->getEntityConfig($entity, 'require');
  $background_image = $this
    ->getEntityBackgroundImage($entity);
  $form['background_image'] = [
    '#type' => 'details',
    '#theme_wrappers' => [
      'details__background_image',
    ],
    '#title' => $this
      ->t('Background Image'),
    '#open' => !$require && $group ? FALSE : TRUE,
    '#group' => $group,
    '#weight' => $group ? NULL : 100,
    '#tree' => TRUE,
  ];
  $form['background_image']['inline_entity_form'] = [
    '#theme_wrappers' => NULL,
    '#type' => 'inline_entity_form',
    '#entity_type' => 'background_image',
    '#langcode' => $entity
      ->language()
      ->getId(),
    '#default_value' => $background_image,
  ];
}