You are here

private function EntityConfigSettingsForm::buildEntitiesBundleForm in Acquia Content Hub 8

Build entities bundle form.

Parameters

string $type: Entity Type.

array $bundle: Entity Bundle.

Return value

array Entities bundle form.

1 call to EntityConfigSettingsForm::buildEntitiesBundleForm()
EntityConfigSettingsForm::buildEntitiesForm in src/Form/EntityConfigSettingsForm.php
Build entities form.

File

src/Form/EntityConfigSettingsForm.php, line 171

Class

EntityConfigSettingsForm
Defines the form to configure the entity types and bundles to be exported.

Namespace

Drupal\acquia_contenthub\Form

Code

private function buildEntitiesBundleForm($type, array $bundle) {

  /** @var \Drupal\acquia_contenthub\Entity\ContentHubEntityTypeConfig $contenthub_entity_config_id */
  $contenthub_entity_config_id = $this->entityManager
    ->getContentHubEntityTypeConfigurationEntity($type);

  // Building the form.
  $form = [];
  foreach ($bundle as $bundle_id => $bundle_name) {
    $view_modes = $this->entityDisplayRepository
      ->getViewModeOptionsByBundle($type, $bundle_id);
    $entity_type_label = $this->entityTypeManager
      ->getDefinition($type)
      ->getLabel();
    $form[$bundle_id] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('%entity_type_label » %bundle_name', [
        '%entity_type_label' => $entity_type_label,
        '%bundle_name' => $bundle_name,
      ]),
      '#collapsible' => TRUE,
    ];
    $enable_viewmodes = FALSE;
    $enable_index = FALSE;
    $rendering = [];
    if ($contenthub_entity_config_id) {
      $enable_viewmodes = $contenthub_entity_config_id
        ->isEnabledViewModes($bundle_id);
      $enable_index = $contenthub_entity_config_id
        ->isEnableIndex($bundle_id);
      $rendering = $contenthub_entity_config_id
        ->getRenderingViewModes($bundle_id);
    }
    $form[$bundle_id]['enable_index'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Publish'),
      '#default_value' => $enable_index,
      '#description' => $this
        ->t("Enable if you want to index this content into Content Hub."),
    ];

    // Preview image is currently only allow for 'node' type.
    if ($type === 'node') {
      $preview_image_link = $this
        ->getContentTypePreviewImageLink($bundle_id);
      $form[$bundle_id]['enable_index']['#description'] .= ' ' . $this
        ->t("Optionally, you can also configure the content's @preview_image_link.", [
        '@preview_image_link' => $preview_image_link,
      ]);
    }
    $form[$bundle_id]['enable_viewmodes'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Publish View modes'),
      '#disabled' => empty($view_modes),
      '#default_value' => empty($view_modes) ? FALSE : $enable_viewmodes,
      '#description' => empty($view_modes) ? $this
        ->t('It is disabled because there are no available view modes. Please enable at least one.') : NULL,
      '#states' => [
        // Only show this field when the 'enable_index' checkbox is enabled.
        'visible' => [
          ':input[name="entities[' . $type . '][' . $bundle_id . '][enable_index]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $title = empty($view_modes) ? NULL : $this
      ->t('Do you want to include the result of any of the following view mode(s)?');
    $default_value = empty($view_modes) || empty($rendering) ? [] : $rendering;
    $first_element = [
      key($view_modes) => key($view_modes),
    ];
    $form[$bundle_id]['rendering'] = [
      '#type' => 'select',
      '#options' => $view_modes,
      '#multiple' => TRUE,
      '#title' => $title,
      '#default_value' => empty($default_value) ? $first_element : $default_value,
      '#states' => [
        'visible' => [
          ':input[name="entities[' . $type . '][' . $bundle_id . '][enable_index]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="entities[' . $type . '][' . $bundle_id . '][enable_viewmodes]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#description' => $this
        ->t('You can hold ctrl (or cmd) key to select multiple view mode(s). Including any of these view modes is usually done in combination with Acquia Lift. Please read the documentation for more information.'),
    ];
  }
  return $form;
}