You are here

public function BackgroundImageForm::buildTypeSelection in Background Image 8

Same name and namespace in other branches
  1. 2.x src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::buildTypeSelection()
  2. 2.0.x src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::buildTypeSelection()

Builds the new type selection step.

Return value

bool TRUE if a valid type has been selected, FALSE otherwise.

1 call to BackgroundImageForm::buildTypeSelection()
BackgroundImageForm::build in src/Form/BackgroundImageForm.php
Builds the Background Image form.

File

src/Form/BackgroundImageForm.php, line 144

Class

BackgroundImageForm

Namespace

Drupal\background_image\Form

Code

public function buildTypeSelection() {

  // Hide the image field.
  $this->subform['image']['#access'] = FALSE;

  // Change submit text to indicate that this is the first step.
  $this->subform['actions']['submit']['#value'] = $this
    ->t('Continue');

  // Type.
  $this->subform['type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Type'),
    '#options' => BackgroundImage::getTypes(),
    '#default_value' => $this
      ->getSubformValue('type', $this->backgroundImage
      ->getType()),
    '#required' => TRUE,
  ];

  // Label.
  $this->subform['label'] = [
    '#access' => !$this->inlineEntity,
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $this
      ->getSubformValue('label', $this->backgroundImage
      ->label()),
  ];
  self::addState($this->subform['label'], [
    'required',
    'visible',
  ], $this->subform['type'], [
    '*' => [
      [
        'value' => BackgroundImageInterface::TYPE_GLOBAL,
      ],
      [
        'value' => BackgroundImageInterface::TYPE_PATH,
      ],
      [
        'value' => BackgroundImageInterface::TYPE_ROUTE,
      ],
    ],
  ]);

  // Entity types.
  $entity_types = [];
  foreach (self::getBackgroundImageManager()
    ->getEnabledEntityTypes() as $id => $entity_type) {

    // Views are handled differently (below), skip.
    if ($id === 'view') {
      continue;
    }
    $entity_types[$id] = $entity_type
      ->getLabel();
  }
  $this->subform['entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity'),
    '#options' => $entity_types,
    '#default_value' => $this
      ->getSubformValue('entity_type'),
  ];
  self::addState($this->subform['entity_type'], [
    'required',
    'visible',
  ], $this->subform['type'], [
    '*' => [
      'value' => BackgroundImageInterface::TYPE_ENTITY,
    ],
  ]);
  foreach (self::getBackgroundImageManager()
    ->getEnabledEntityTypes() as $id => $entity_type) {

    // Views are handled differently (below), skip.
    if ($id === 'view') {
      continue;
    }
    $this->subform["entity_type_{$id}"] = [
      '#type' => 'entity_autocomplete',
      '#title' => $entity_type
        ->getLabel(),
      '#target_type' => $id,
      '#default_value' => $this
        ->getSubformValue("entity_type_{$id}"),
    ];

    // Limit to only enabled bundles, if necessary.
    if ($entity_type
      ->getBundleEntityType()) {
      $this->subform["entity_type_{$id}"]['#selection_settings'] = [
        'target_bundles' => array_keys(self::getBackgroundImageManager()
          ->getEnabledEntityTypeBundles($entity_type)),
      ];
    }
    self::addState($this->subform["entity_type_{$id}"], [
      'required',
      'visible',
    ], $this->subform['type'], [
      '*' => [
        'value' => BackgroundImageInterface::TYPE_ENTITY,
      ],
    ]);
    self::addState($this->subform["entity_type_{$id}"], [
      'required',
      'visible',
    ], $this->subform['entity_type'], [
      '*' => [
        'value' => $id,
      ],
    ]);
  }

  // Entity Bundle types.
  $entity_bundles = [];
  foreach (self::getBackgroundImageManager()
    ->getEnabledEntityTypes() as $id => $entity_type) {

    // Views are handled differently (below), skip.
    if ($id === 'view') {
      continue;
    }
    $entity_type_id = $id;
    $entity_type_label = $entity_type
      ->getLabel();
    $bundles = self::getBackgroundImageManager()
      ->getEnabledEntityTypeBundles($entity_type);
    foreach ($bundles as $bundle => $info) {
      $entity_bundles["{$entity_type_id}:{$bundle}"] = $entity_type_id === $bundle ? $info['label'] : "{$info['label']} ({$entity_type_label})";
    }
  }
  $this->subform['entity_bundle'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Bundle'),
    '#options' => $entity_bundles,
    '#default_value' => $this
      ->getSubformValue('entity_bundle', $this->type === BackgroundImageInterface::TYPE_ENTITY_BUNDLE ? $this->target : NULL),
  ];
  self::addState($this->subform['entity_bundle'], [
    'required',
    'visible',
  ], $this->subform['type'], [
    '*' => [
      'value' => BackgroundImageInterface::TYPE_ENTITY_BUNDLE,
    ],
  ]);

  // Path.
  $this->subform['path'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Path'),
    '#description' => $this
      ->t('Valid internal path(s), separated by new lines. Use % as a wild card if needed.'),
    '#default_value' => $this
      ->getSubformValue('path', $this->type === BackgroundImageInterface::TYPE_PATH ? $this->target : NULL),
  ];
  self::addState($this->subform['path'], [
    'required',
    'visible',
  ], $this->subform['type'], [
    '*' => [
      'value' => BackgroundImageInterface::TYPE_PATH,
    ],
  ]);

  // Route.
  $this->subform['route'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Route'),
    '#description' => $this
      ->t('Valid route name(s), separated by new lines.'),
    '#default_value' => $this
      ->getSubformValue('route', $this->type === BackgroundImageInterface::TYPE_ROUTE ? $this->target : NULL),
  ];
  self::addState($this->subform['route'], [
    'required',
    'visible',
  ], $this->subform['type'], [
    '*' => [
      'value' => BackgroundImageInterface::TYPE_ROUTE,
    ],
  ]);

  // View.
  $this->subform['view'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('View'),
    '#description' => $this
      ->t('A specific View page display.'),
    '#options' => self::getBackgroundImageManager()
      ->getViewsPages(),
    '#default_value' => $this
      ->getSubformValue('view', $this->type === BackgroundImageInterface::TYPE_VIEW ? $this->target : NULL),
  ];
  self::addState($this->subform['view'], [
    'required',
    'visible',
  ], $this->subform['type'], [
    '*' => [
      'value' => BackgroundImageInterface::TYPE_VIEW,
    ],
  ]);
  return FALSE;
}