You are here

class ConvertBundlesForm in Convert Bundles 8

ConvertBundlesForm.

Hierarchy

Expanded class hierarchy of ConvertBundlesForm

1 string reference to 'ConvertBundlesForm'
convert_bundles.routing.yml in ./convert_bundles.routing.yml
convert_bundles.routing.yml

File

src/Form/ConvertBundlesForm.php, line 23

Namespace

Drupal\convert_bundles\Form
View source
class ConvertBundlesForm extends FormBase implements FormInterface {

  /**
   * Set a var to make stepthrough form.
   *
   * @var step
   */
  protected $step = 1;

  /**
   * Set some entity vars.
   *
   * @var entities
   */
  protected $entities = NULL;

  /**
   * Set some content type vars.
   *
   * @var entityType
   */
  protected $entityType = NULL;

  /**
   * Set some content type vars.
   *
   * @var allBundles
   */
  protected $allBundles = NULL;

  /**
   * Set some content type vars.
   *
   * @var fromType
   */
  protected $fromType = NULL;

  /**
   * Set some content type vars.
   *
   * @var toType
   */
  protected $toType = NULL;

  /**
   * Set field vars.
   *
   * @var fieldsFrom
   */
  protected $fieldsFrom = NULL;

  /**
   * Set field vars.
   *
   * @var fieldsTo
   */
  protected $fieldsTo = NULL;

  /**
   * Create new based on to content type.
   *
   * @var createNew
   */
  protected $createNew = NULL;

  /**
   * Create new based on to content type.
   *
   * @var fields_new_to
   */
  protected $fieldsNewTo = NULL;

  /**
   * Keep track of user input.
   *
   * @var userInput
   */
  protected $userInput = [];

  /**
   * The entity field manager service.
   *
   * @var \Drupal\Core\Entity\EntityFieldManager
   */
  protected $entityFieldManager;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * The entity bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfo
   */
  protected $bundleInfo;

  /**
   * Tempstorage.
   *
   * @var tempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * Session.
   *
   * @var sessionManager
   */
  private $sessionManager;

  /**
   * User.
   *
   * @var currentUser
   */
  protected $currentUser;

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The route builder.
   *
   * @var \Drupal\Core\Routing\RouteBuilderInterface
   */
  protected $routeBuilder;

  /**
   * Constructs a \Drupal\convert_bundles\Form\ConvertBundlesForm.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   Temp storage.
   * @param \Drupal\Core\Session\SessionManagerInterface $session_manager
   *   Session.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   User.
   * @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
   *   EntityFieldManager.
   * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
   *   EntityTypeManager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfo $bundle_info
   *   EntityTypeBundleInfo.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   RouteMatch.
   * @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
   *   Route.
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory, SessionManagerInterface $session_manager, AccountInterface $current_user, EntityFieldManager $entity_field_manager, EntityTypeManager $entity_type_manager, EntityTypeBundleInfo $bundle_info, RouteMatchInterface $route_match, RouteBuilderInterface $route_builder) {
    $this->tempStoreFactory = $temp_store_factory;
    $this->sessionManager = $session_manager;
    $this->currentUser = $current_user
      ->id();
    $this->entityFieldManager = $entity_field_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->bundleInfo = $bundle_info;
    $this->routeMatch = $route_match;
    $this->routeBuilder = $route_builder;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('tempstore.private'), $container
      ->get('session_manager'), $container
      ->get('current_user'), $container
      ->get('entity_field.manager'), $container
      ->get('entity_type.manager'), $container
      ->get("entity_type.bundle.info"), $container
      ->get('current_route_match'), $container
      ->get('router.builder'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'convert_bundles_form';
  }

  /**
   * {@inheritdoc}
   */
  public function convertBundles() {
    $base_table_names = ConvertBundles::getBaseTableNames($this->entityType);
    $userInput = ConvertBundles::sortUserInput($this->userInput, $this->fieldsNewTo, $this->fieldsFrom);
    $map_fields = $userInput['map_fields'];
    $update_fields = $userInput['update_fields'];
    $field_table_names = ConvertBundles::getFieldTableNames($this->entityType, $this->fieldsFrom);
    $ids = array_keys($this->entities);
    $limit = 100;
    $batch = [
      'title' => $this
        ->t('Converting Base Tables...'),
      'operations' => [
        [
          '\\Drupal\\convert_bundles\\ConvertBundles::convertBaseTables',
          [
            $this->entityType,
            $base_table_names,
            $ids,
            $this->toType,
          ],
        ],
        [
          '\\Drupal\\convert_bundles\\ConvertBundles::convertFieldTables',
          [
            $field_table_names,
            $ids,
            $this->toType,
            $update_fields,
          ],
        ],
        [
          '\\Drupal\\convert_bundles\\ConvertBundles::addNewFields',
          [
            $this->entityType,
            $ids,
            $limit,
            $map_fields,
            $this->fieldsTo,
            $this->entities,
          ],
        ],
      ],
      'finished' => '\\Drupal\\convert_bundles\\ConvertBundles::ConvertBundlesFinishedCallback',
    ];
    batch_set($batch);
    $this->tempStoreFactory
      ->get('convert_bundles_ids')
      ->delete($this->currentUser);
    return 'Selected entities of type ' . implode(', ', $this->fromType) . ' were converted to ' . $this->toType;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    switch ($this->step) {
      case 1:
        $form_state
          ->setRebuild();
        $this->entityType = $form_state
          ->getValues()['convert_entity_type'];
        break;
      case 2:
        $form_state
          ->setRebuild();
        $this->fromType = array_filter($form_state
          ->getValues()['convert_bundles_from']);
        if (empty($this->entities)) {
          $this->entities = ConvertBundles::getEntities($this->entityType, $this->fromType);
        }
        break;
      case 3:
        $form_state
          ->setRebuild();
        $this->toType = $form_state
          ->getValues()['convert_bundles_to'];
        break;
      case 4:
        $form_state
          ->setRebuild();
        $data_to_process = array_diff_key($form_state
          ->getValues(), array_flip([
          'op',
          'submit',
          'form_id',
          'form_build_id',
          'form_token',
        ]));
        $this->userInput = $data_to_process;
        break;
      case 5:
        $this->createNew = $form['create_new']['#value'];
        if (!$this->createNew) {
          $this->step++;
          goto six;
        }
        $form_state
          ->setRebuild();
        break;
      case 6:
        $values = $form_state
          ->getValues()['default_value_input'];
        foreach ($values as $key => $value) {
          unset($values[$key]['add_more']);
          if (isset($value['value'])) {
            $values[$key] = $value['value'];
          }
        }
        $data_to_process = array_diff_key($values, array_flip([
          'op',
          'submit',
          'form_id',
          'form_build_id',
          'form_token',
        ]));
        $this->userInput = array_merge($this->userInput, $data_to_process);

        // Used also for goto.
        six:
        $form_state
          ->setRebuild();
        break;
      case 7:
        if (method_exists($this, 'ConvertBundles')) {
          $return_verify = $this
            ->ConvertBundles();
        }
        \Drupal::messenger()
          ->addStatus($return_verify);
        $this->routeBuilder
          ->rebuild();
        break;
    }
    $this->step++;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    if (isset($this->form)) {
      $form = $this->form;
    }
    $form['#title'] = $this
      ->t('Convert Bundles');
    $submit_label = 'Next';
    switch ($this->step) {
      case 1:
        if ($this->routeMatch
          ->getRouteName() == 'convert_bundles.admin') {
          $this->tempStoreFactory
            ->get('convert_bundles_ids')
            ->delete($this->currentUser);
        }

        // Retrieve IDs from the temporary storage.
        $this->entities = $this->tempStoreFactory
          ->get('convert_bundles_ids')
          ->get($this->currentUser);
        if (!empty($this->entities)) {
          $this->step++;
          goto two;
        }
        $options = [];
        foreach (array_keys($this->entityTypeManager
          ->getDefinitions()) as $entity_type) {
          $bundles = $this->bundleInfo
            ->getAllBundleInfo();
          $storage = \Drupal::service('entity_type.manager')
            ->getStorage($entity_type);
          if (isset($bundles[$entity_type]) && !empty($bundles[$entity_type]) && count($bundles[$entity_type]) > 1 && method_exists($storage, 'getBaseTable') && method_exists($storage, 'getDataTable')) {
            $options[$entity_type] = $entity_type;
          }
        }
        $header = [
          'type_names' => $this
            ->t('Entity Types'),
        ];
        $form['#title'] .= ' - ' . $this
          ->t('Select Entity Type to Convert (Only Entity Types with multiple bundles are shown)');
        $form['convert_entity_type'] = [
          '#type' => 'select',
          '#header' => $header,
          '#options' => $options,
          '#empty' => $this
            ->t('No convertable types found'),
        ];
        break;
      case 2:
        two:
        $options = [];
        $bundle_info = $this->bundleInfo
          ->getAllBundleInfo();
        $bundles = [];
        if (!empty($this->entities)) {
          $entity_types = [];
          foreach ($this->entities as $entity) {
            $bundles[] = $entity
              ->bundle();
            $entity_types[] = $entity
              ->getEntityTypeId();
          }
          if (count($entity_type = array_unique($entity_types)) > 1) {
            \Drupal::messenger()
              ->addError($this
              ->t('We cant convert multiple types of entities at once'));
          }
          $this->entityType = $entity_type[0];
        }
        $all_bundles = $bundle_info[$this->entityType];
        foreach ($all_bundles as $machine_name => $bundle_array) {
          $this->allBundles[$machine_name] = $bundle_array['label'];
        }
        if (empty($bundles)) {
          $bundles = array_keys($all_bundles);
        }
        if (count($bundles) == 1) {
          $this->step++;
          $this->fromType = $bundles;
          goto three;
        }
        foreach (array_unique($bundles) as $bundle) {
          $options[$bundle]['bundle_names'] = $all_bundles[$bundle]['label'];
        }
        $header = [
          'bundle_names' => $this
            ->t('Bundle Name(s)'),
        ];
        $form['#title'] .= ' - ' . $this
          ->t('Select Bundles to Convert (Bundles are from all entities selected)');
        $form['convert_bundles_from'] = [
          '#type' => 'tableselect',
          '#header' => $header,
          '#options' => $options,
          '#empty' => $this
            ->t('No bundles found'),
        ];
        break;
      case 3:
        three:
        $form['#title'] .= ' - ' . $this
          ->t('Select the Bundle to Convert Selected Entities to');
        $form['convert_bundles_to'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('To Bundle'),
          '#options' => $this->allBundles,
        ];
        break;
      case 4:

        // Get the fields.
        $entityFieldManager = $this->entityFieldManager;
        foreach ($this->fromType as $from_bundle) {
          $this->fieldsFrom[$from_bundle] = $entityFieldManager
            ->getFieldDefinitions($this->entityType, $from_bundle);
        }
        $this->fieldsTo = $entityFieldManager
          ->getFieldDefinitions($this->entityType, $this->toType);
        $fields_to = ConvertBundles::getToFields($this->fieldsTo);
        $fields_to_names = $fields_to['fields_to_names'];
        $fields_to_types = $fields_to['fields_to_types'];
        $fields_from = ConvertBundles::getFromFields($this->fieldsFrom, $fields_to_names, $fields_to_types);
        $fields_from_names = $fields_from['fields_from_names'];
        $fields_from_form = $fields_from['fields_from_form'];

        // Find missing fields. allowing values to be input later.
        $fields_to_names = array_diff($fields_to_names, [
          'remove',
          'append_to_body',
        ]);
        $this->fieldsNewTo = array_diff(array_keys($fields_to_names), $fields_from_names);
        $form = array_merge($form, $fields_from_form);
        $form['actions']['submit'] = [
          '#type' => 'submit',
          '#value' => $this
            ->t('Next'),
          '#button_type' => 'primary',
        ];
        break;
      case 5:
        $form['create_new'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Create field values for new fields in target content type'),
        ];
        $form['actions']['submit'] = [
          '#type' => 'submit',
          '#value' => $this
            ->t('Next'),
          '#button_type' => 'primary',
        ];
        break;
      case 6:

        // Put the to fields in the form for new values.
        foreach ($this->fieldsNewTo as $field_name) {
          if (!in_array($field_name, $this->userInput)) {

            // TODO - Date widgets are relative. Fix.
            // Create an arbitrary entity object.
            $ids = (object) [
              'entity_type' => $this->entityType,
              'bundle' => $this->toType,
              'entity_id' => NULL,
            ];
            $fake_entity = _field_create_entity_from_ids($ids);
            $items = $fake_entity
              ->get($field_name);
            $temp_form_element = [];
            $temp_form_state = new FormState();
            $form[$field_name] = $items
              ->defaultValuesForm($temp_form_element, $temp_form_state);
          }
        }
        $form['actions']['submit'] = [
          '#type' => 'submit',
          '#value' => $this
            ->t('Next'),
          '#button_type' => 'primary',
        ];
        break;
      case 7:
        $count = \Drupal::translation()
          ->formatPlural(count($this->entities), '1 selected entity', '@count selected entities');
        $from_types = implode(', ', $this->fromType);
        \Drupal::messenger()
          ->addWarning($this
          ->t('Are you sure you want to convert @target of type <em>@from_type</em> to type <em>@to_type</em>?', [
          '@target' => $count,
          '@from_type' => $from_types,
          '@to_type' => $this->toType,
        ]));
        $form['actions']['submit'] = [
          '#type' => 'submit',
          '#value' => $this
            ->t('Convert'),
          '#button_type' => 'primary',
        ];
        break;
    }
    \Drupal::messenger()
      ->addWarning($this
      ->t('This module is experimental. PLEASE do not use on production databases without prior testing and a complete database dump.'));
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $submit_label,
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    switch ($this->step) {
      case 2:
        if (empty(array_filter($form_state
          ->getValues()['convert_bundles_from']))) {
          $form_state
            ->setErrorByName('convert_bundles_from', $this
            ->t('No bundles selected.'));
        }
        break;
      default:

        // TODO - validate other steps.
        break;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConvertBundlesForm::$allBundles protected property Set some content type vars.
ConvertBundlesForm::$bundleInfo protected property The entity bundle info service.
ConvertBundlesForm::$createNew protected property Create new based on to content type.
ConvertBundlesForm::$currentUser protected property User.
ConvertBundlesForm::$entities protected property Set some entity vars.
ConvertBundlesForm::$entityFieldManager protected property The entity field manager service.
ConvertBundlesForm::$entityType protected property Set some content type vars.
ConvertBundlesForm::$entityTypeManager protected property The entity type manager service.
ConvertBundlesForm::$fieldsFrom protected property Set field vars.
ConvertBundlesForm::$fieldsNewTo protected property Create new based on to content type.
ConvertBundlesForm::$fieldsTo protected property Set field vars.
ConvertBundlesForm::$fromType protected property Set some content type vars.
ConvertBundlesForm::$routeBuilder protected property The route builder.
ConvertBundlesForm::$routeMatch protected property The route match. Overrides FormBase::$routeMatch
ConvertBundlesForm::$sessionManager private property Session.
ConvertBundlesForm::$step protected property Set a var to make stepthrough form.
ConvertBundlesForm::$tempStoreFactory protected property Tempstorage.
ConvertBundlesForm::$toType protected property Set some content type vars.
ConvertBundlesForm::$userInput protected property Keep track of user input.
ConvertBundlesForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
ConvertBundlesForm::convertBundles public function
ConvertBundlesForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ConvertBundlesForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ConvertBundlesForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ConvertBundlesForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ConvertBundlesForm::__construct public function Constructs a \Drupal\convert_bundles\Form\ConvertBundlesForm.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.