You are here

protected function FlowForm::renderEnabledBundle in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Form/FlowForm.php \Drupal\cms_content_sync\Form\FlowForm::renderEnabledBundle()
  2. 2.0.x src/Form/FlowForm.php \Drupal\cms_content_sync\Form\FlowForm::renderEnabledBundle()

Render the bundle edit form.

Parameters

$type_key:

$entity_bundle_name:

Return value

array

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to FlowForm::renderEnabledBundle()
FlowForm::renderEntityType in src/Form/FlowForm.php
Render all bundles for the given entity type. Displayed in vertical tabs from the parent form.

File

src/Form/FlowForm.php, line 1123

Class

FlowForm
Form handler for the Flow add and edit forms.

Namespace

Drupal\cms_content_sync\Form

Code

protected function renderEnabledBundle(FormStateInterface $form_state, $type_key, $entity_bundle_name) {
  $entity_types = $this->bundleInfoService
    ->getAllBundleInfo();
  $entity_bundle = $entity_types[$type_key][$entity_bundle_name];
  $bundle_id = $type_key . '-' . $entity_bundle_name;
  $settings_id = 'sync-entities-' . $bundle_id;
  $type = $this
    ->getCurrentFormType();
  $allow_push = Flow::TYPE_PUSH === $type || Flow::TYPE_BOTH === $type;
  $allow_pull = Flow::TYPE_PULL === $type || Flow::TYPE_BOTH === $type;
  $current_values = $this
    ->getCurrentValues($form_state);

  // Before a flow can be created, at least one pool must exist.
  // Get all pool entities.
  $pool_entities = Pool::getAll();
  $push_option_labels = [
    PushIntent::PUSH_DISABLED => $this
      ->t('Disabled')
      ->render(),
    PushIntent::PUSH_AUTOMATICALLY => $this
      ->t('All')
      ->render(),
    PushIntent::PUSH_AS_DEPENDENCY => $this
      ->t('Referenced')
      ->render(),
    PushIntent::PUSH_MANUALLY => $this
      ->t('Manually')
      ->render(),
  ];
  $pull_option_labels = [
    PullIntent::PULL_DISABLED => $this
      ->t('Disabled')
      ->render(),
    PullIntent::PULL_AUTOMATICALLY => $this
      ->t('All')
      ->render(),
    PullIntent::PULL_AS_DEPENDENCY => $this
      ->t('Referenced')
      ->render(),
    PullIntent::PULL_MANUALLY => $this
      ->t('Manually')
      ->render(),
  ];
  $def_sync_entities = $this
    ->getCurrentValues($form_state)['sync_entities'];
  $display_modes = $this->entityTypeManager
    ->getStorage('entity_view_display')
    ->loadMultiple();
  $display_modes_ids = array_keys($display_modes);
  $version = Flow::getEntityTypeVersion($type_key, $entity_bundle_name);
  $entity_push_bundle_row = [];
  $available_preview_modes = [];
  foreach ($display_modes_ids as $id) {
    $length = strlen($type_key) + strlen($entity_bundle_name) + 2;
    if (substr($id, 0, $length) != $type_key . '.' . $entity_bundle_name . '.') {
      continue;
    }
    $id = substr($id, $length);
    $label = $id;
    $available_preview_modes[$id] = $label;
  }
  if (!isset($def_sync_entities[$type_key . '-' . $entity_bundle_name])) {
    $row_default_values = [
      'id' => $type_key . '-' . $entity_bundle_name,
      'export' => $allow_push ? 'node' === $type_key ? PushIntent::PUSH_AUTOMATICALLY : PushIntent::PUSH_AS_DEPENDENCY : null,
      'export_deletion_settings' => [
        'export_deletion' => true,
      ],
      'import' => $allow_pull ? 'node' === $type_key ? PullIntent::PULL_AUTOMATICALLY : PullIntent::PULL_AS_DEPENDENCY : null,
      'import_deletion_settings' => [
        'import_deletion' => true,
        'allow_local_deletion_of_import' => false,
      ],
      'handler_settings' => [],
      'import_updates' => PullIntent::PULL_UPDATE_FORCE,
      'preview' => Flow::PREVIEW_DISABLED,
      'display_name' => $this
        ->t('@bundle', [
        '@bundle' => $entity_bundle['label'],
      ])
        ->render(),
      'entity_type' => $type_key,
      'entity_bundle' => $entity_bundle_name,
      'pool_export_widget_type' => 'checkboxes',
    ];
    foreach ($pool_entities as $pool) {
      $row_default_values['export_pools'][$pool
        ->id()] = PushIntent::PUSH_AUTOMATICALLY === $row_default_values['export'] ? Pool::POOL_USAGE_FORCE : Pool::POOL_USAGE_ALLOW;
      $row_default_values['import_pools'][$pool
        ->id()] = Pool::POOL_USAGE_FORCE;
    }
  }
  else {
    $row_default_values = $def_sync_entities[$type_key . '-' . $entity_bundle_name];
    if (empty($row_default_values['export']) || PushIntent::PUSH_DISABLED === $row_default_values['export']) {
      if (!$allow_pull) {
        $row_default_values['export'] = 'node' === $type_key ? PushIntent::PUSH_AUTOMATICALLY : PushIntent::PUSH_AS_DEPENDENCY;
      }
    }
    if (empty($row_default_values['import']) || PullIntent::PULL_DISABLED === $row_default_values['import']) {
      if (!$allow_push) {
        $row_default_values['import'] = 'node' === $type_key ? PullIntent::PULL_AUTOMATICALLY : PullIntent::PULL_AS_DEPENDENCY;
      }
    }
  }
  $entity_handlers = $this->entityPluginManager
    ->getHandlerOptions($type_key, $entity_bundle_name, true);
  $entity_handler_names = array_keys($entity_handlers);
  $handler_id = empty($row_default_values['handler']) || 'ignore' === $row_default_values['handler'] ? reset($entity_handler_names) : $row_default_values['handler'];
  $bundle_info = [
    '#type' => 'details',
    '#prefix' => '<div id="' . $settings_id . '">',
    '#suffix' => '</div>',
    '#open' => true,
    '#title' => $this
      ->t('@bundle (@machine_name)', [
      '@bundle' => $entity_bundle['label'],
      '@machine_name' => $entity_bundle_name,
    ]),
    'version' => [
      '#markup' => '<small>Entity type - bundle version: ' . $version . '</small>' . (empty($row_default_values['version']) || $version == $row_default_values['version'] ? '' : '<br><strong>Changed from ' . $row_default_values['version'] . '</strong>'),
    ],
  ];
  $bundle_info['edit'] = [
    '#type' => 'hidden',
    '#value' => '1',
  ];
  $bundle_info['handler'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Handler'),
    '#title_display' => 'invisible',
    '#options' => $entity_handlers,
    '#default_value' => $handler_id,
  ];

  /**
   * @var \Drupal\cms_content_sync\Plugin\EntityHandlerInterface $handler
   */
  $handler = $this->entityPluginManager
    ->createInstance($handler_id, [
    'entity_type_name' => $type_key,
    'bundle_name' => $entity_bundle_name,
    'settings' => $row_default_values,
    'sync' => null,
  ]);
  $allowed_push_options = $handler
    ->getAllowedPushOptions();
  $push_options = [];
  foreach ($allowed_push_options as $option) {
    $push_options[$option] = $push_option_labels[$option];
  }
  if (!$allow_pull) {
    unset($push_options[PushIntent::PUSH_DISABLED]);
  }
  $bundle_info['disable'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Disable'),
    '#name' => 'disable-' . $type_key . '-' . $entity_bundle_name,
    '#submit' => [
      '::disableBundle',
    ],
    '#entity_type' => $type_key,
    '#bundle' => $entity_bundle_name,
    '#limit_validation_errors' => [],
    '#attributes' => [
      'class' => [
        'button--danger',
      ],
    ],
    '#ajax' => [
      'callback' => '::ajaxReturn',
      'wrapper' => $settings_id,
      'method' => 'replace',
      'effect' => 'fade',
      'progress' => [
        'type' => 'throbber',
        'message' => 'loading settings...',
      ],
    ],
  ];
  if ('1' === $form_state
    ->getValue([
    $type_key,
    $entity_bundle_name,
    'show-version-mismatches',
  ])) {
    $bundle_info['version_mismatch_' . $type_key . '_' . $entity_bundle_name] = _cms_content_sync_display_version_mismatches(null, $form_state);
  }
  else {
    $bundle_info['version_mismatch_' . $type_key . '_' . $entity_bundle_name] = [
      '#type' => 'submit',
      '#value' => t('Show version mismatches'),
      '#name' => 'version-' . $type_key . '-' . $entity_bundle_name,
      '#submit' => [
        '::showVersionMismatches',
      ],
      '#entity_type' => $type_key,
      '#bundle' => $entity_bundle_name,
      '#recursive' => false,
      '#limit_validation_errors' => [],
      '#ajax' => [
        'callback' => '::ajaxReturn',
        'wrapper' => $settings_id,
        'method' => 'replace',
        'effect' => 'fade',
        'progress' => [
          'type' => 'throbber',
          'message' => 'loading settings...',
        ],
      ],
    ];
  }
  if ('ignore' != $handler_id) {
    $advanced_settings = $handler
      ->getHandlerSettings(isset($current_values['sync_entities'][$bundle_id]['handler_settings']) ? $current_values['sync_entities'][$bundle_id]['handler_settings'] : [], $this
      ->getCurrentFormType());
    if (count($advanced_settings)) {
      $bundle_info['handler_settings'] = array_merge([
        '#type' => 'container',
      ], $advanced_settings);
    }
  }
  if (!isset($bundle_info['handler_settings'])) {
    $bundle_info['handler_settings'] = [
      '#markup' => '<br><br>',
    ];
  }
  if ($allow_push) {
    $entity_push_bundle_row['export'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Push'),
      '#title_display' => 'invisible',
      '#options' => $push_options,
      '#default_value' => $row_default_values['export'],
    ];
    foreach ($pool_entities as $pool) {
      $entity_push_bundle_row['export_pools'][$pool
        ->id()] = [
        '#type' => 'select',
        '#title' => $this
          ->t($pool
          ->label()),
        '#options' => [
          Pool::POOL_USAGE_FORCE => $this
            ->t('Force'),
          Pool::POOL_USAGE_ALLOW => $this
            ->t('Allow'),
          Pool::POOL_USAGE_FORBID => $this
            ->t('Forbid'),
        ],
        '#default_value' => isset($row_default_values['export_pools'][$pool
          ->id()]) ? $row_default_values['export_pools'][$pool
          ->id()] : ($this->entity
          ->id() ? Pool::POOL_USAGE_FORBID : (PushIntent::PUSH_AUTOMATICALLY === $row_default_values['export'] ? Pool::POOL_USAGE_FORCE : Pool::POOL_USAGE_ALLOW)),
      ];
    }
    $entity_push_bundle_row['export_deletion_settings']['export_deletion'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Push deletion'),
      '#default_value' => isset($row_default_values['export_deletion_settings']['export_deletion']) && 1 == $row_default_values['export_deletion_settings']['export_deletion'],
    ];
    $entity_push_bundle_row['pool_export_widget_type'] = [
      '#type' => 'select',
      '#options' => [
        'checkboxes' => $this
          ->t('Checkboxes'),
        'radios' => $this
          ->t('Radio boxes'),
        'single_select' => $this
          ->t('Single select'),
        'multi_select' => $this
          ->t('Multi select'),
      ],
      '#default_value' => isset($row_default_values['pool_export_widget_type']) ? $row_default_values['pool_export_widget_type'] : 'checkboxes',
    ];
    $options = array_merge([
      Flow::PREVIEW_DISABLED => $this
        ->t('Disabled')
        ->render(),
    ], 'ignore' == $handler_id ? [] : array_merge([
      Flow::PREVIEW_TABLE => $this
        ->t('Default')
        ->render(),
    ], $available_preview_modes));
    $default = 'ignore' == $handler_id ? Flow::PREVIEW_DISABLED : Flow::PREVIEW_TABLE;
    $entity_push_bundle_row['preview'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Preview'),
      '#title_display' => 'invisible',
      '#options' => $options,
      '#default_value' => isset($row_default_values['preview']) || 'ignore' == $handler_id ? $row_default_values['preview'] : $default,
      '#description' => $this
        ->t('Make sure to go to the general "Settings" and enable previews to make use of this.'),
    ];
    $entity_push_table = [
      '#type' => 'table',
      '#sticky' => true,
      '#header' => [
        $this
          ->t('Push'),
        $this
          ->t('Push to pools'),
        $this
          ->t('Push deletions'),
        $this
          ->t('Pool widget'),
        $this
          ->t('Preview'),
      ],
      $type_key . '-' . $entity_bundle_name => $entity_push_bundle_row,
    ];
  }
  if ($allow_pull) {
    $allowed_pull_options = $handler
      ->getAllowedPullOptions();
    $pull_options = [];
    foreach ($allowed_pull_options as $option) {
      $pull_options[$option] = $pull_option_labels[$option];
    }
    if (!$allow_push) {
      unset($pull_options[PullIntent::PULL_DISABLED]);
    }
    $entity_pull_bundle_row['import'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Pull'),
      '#title_display' => 'invisible',
      '#options' => $pull_options,
      '#default_value' => $row_default_values['import'],
    ];
    foreach ($pool_entities as $pool) {
      $entity_pull_bundle_row['import_pools'][$pool
        ->id()] = [
        '#type' => 'select',
        '#title' => $this
          ->t($pool
          ->label()),
        '#options' => [
          Pool::POOL_USAGE_FORCE => $this
            ->t('Force'),
          Pool::POOL_USAGE_ALLOW => $this
            ->t('Allow'),
          Pool::POOL_USAGE_FORBID => $this
            ->t('Forbid'),
        ],
        '#default_value' => isset($row_default_values['import_pools'][$pool
          ->id()]) ? $row_default_values['import_pools'][$pool
          ->id()] : ($this->entity
          ->id() ? Pool::POOL_USAGE_FORBID : Pool::POOL_USAGE_FORCE),
      ];
    }
    $entity_pull_bundle_row['import_deletion_settings']['import_deletion'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Pull deletion'),
      '#default_value' => isset($row_default_values['import_deletion_settings']['import_deletion']) && 1 == $row_default_values['import_deletion_settings']['import_deletion'],
    ];
    $entity_pull_bundle_row['import_deletion_settings']['allow_local_deletion_of_import'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow deletion of pulled content'),
      '#default_value' => isset($row_default_values['import_deletion_settings']['allow_local_deletion_of_import']) && 1 == $row_default_values['import_deletion_settings']['allow_local_deletion_of_import'],
    ];
    $entity_pull_bundle_row['import_updates'] = [
      '#type' => 'select',
      '#options' => [
        PullIntent::PULL_UPDATE_FORCE => $this
          ->t('Dismiss local changes'),
        PullIntent::PULL_UPDATE_IGNORE => $this
          ->t('Ignore updates completely'),
        PullIntent::PULL_UPDATE_FORCE_AND_FORBID_EDITING => $this
          ->t('Forbid local changes and update'),
        PullIntent::PULL_UPDATE_FORCE_UNLESS_OVERRIDDEN => $this
          ->t('Update unless overwritten locally'),
      ],
      '#default_value' => isset($row_default_values['import_updates']) ? $row_default_values['import_updates'] : null,
    ];
    if ('node' === $type_key) {
      $bundle_type = $this->entityTypeManager
        ->getStorage('node_type')
        ->load($entity_bundle_name);
      if ($bundle_type instanceof RevisionableEntityBundleInterface && $bundle_type
        ->shouldCreateNewRevision()) {
        $entity_pull_bundle_row['import_updates']['#options'][PullIntent::PULL_UPDATE_UNPUBLISHED] = $this
          ->t('Create unpublished revisions');
      }
    }
    $entity_pull_table = [
      '#type' => 'table',
      '#sticky' => true,
      '#header' => [
        $this
          ->t('Pull'),
        $this
          ->t('Pull from pool'),
        $this
          ->t('Pull deletions'),
        $this
          ->t('Pull updates'),
      ],
      $type_key . '-' . $entity_bundle_name => $entity_pull_bundle_row,
    ];
  }
  $show_all_fields = $form_state
    ->getValue([
    $type_key,
    $entity_bundle_name,
    'fields',
    'advanced',
    'show-all',
  ]);
  $entity_field_table = $this
    ->renderFields($form_state, $type_key, $entity_bundle_name, '1' === $show_all_fields);
  if (isset($entity_push_table)) {
    $bundle_info['export'] = $entity_push_table;
  }
  if (isset($entity_pull_table)) {
    $bundle_info['import'] = $entity_pull_table;
  }
  if (isset($entity_field_table)) {
    $bundle_info['fields'] = $entity_field_table;
  }
  return $bundle_info;
}