You are here

class BulkUpdateFieldsForm in Bulk Update Fields 8.2

Same name and namespace in other branches
  1. 8 src/Form/BulkUpdateFieldsForm.php \Drupal\bulk_update_fields\Form\BulkUpdateFieldsForm

BulkUpdateFieldsForm.

Hierarchy

Expanded class hierarchy of BulkUpdateFieldsForm

1 string reference to 'BulkUpdateFieldsForm'
bulk_update_fields.routing.yml in ./bulk_update_fields.routing.yml
bulk_update_fields.routing.yml

File

src/Form/BulkUpdateFieldsForm.php, line 19

Namespace

Drupal\bulk_update_fields\Form
View source
class BulkUpdateFieldsForm extends FormBase {

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

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

  /**
   * Tempstorage.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * Session.
   *
   * @var \Drupal\Core\Session\SessionManagerInterface
   */
  private $sessionManager;

  /**
   * User.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  private $currentUser;

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

  /**
   * Constructs a \Drupal\bulk_update_fields\Form\BulkUpdateFieldsForm.
   *
   * @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\Routing\RouteBuilderInterface $route_builder
   *   Route.
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory, SessionManagerInterface $session_manager, AccountInterface $current_user, RouteBuilderInterface $route_builder) {
    $this->tempStoreFactory = $temp_store_factory;
    $this->sessionManager = $session_manager;
    $this->currentUser = $current_user;
    $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('router.builder'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function updateFields() {
    $entities = $this->userInput['entities'];
    $fields = $this->userInput['fields'];
    $operations = [];
    foreach ($entities as $entity) {
      $operations[] = [
        '\\Drupal\\bulk_update_fields\\BulkUpdateFields::updateFields',
        [
          $entity,
          $fields,
        ],
      ];
    }
    $batch = [
      'title' => $this
        ->t('Updating Fields...'),
      'operations' => $operations,
      'finished' => '\\Drupal\\bulk_update_fields\\BulkUpdateFields::bulkUpdateFieldsFinishedCallback',
      'file' => '\\Drupal\\bulk_update_fields\\BulkUpdateFields',
    ];
    batch_set($batch);
    return 'All fields were updated successfully';
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    switch ($this->step) {
      case 1:
        $this->userInput['fields'] = array_filter($form_state
          ->getValues()['table']);
        $form_state
          ->setRebuild();
        break;
      case 2:
        $form_state_values = $form_state
          ->getValues();
        foreach ($form_state_values as $field_name => $form_state_value) {

          // Paragraphs dont allow defaults.
          // Force it with just values.
          if ($field_name != 'default_value_input' && is_array($form_state_value)) {
            $form_state_values['default_value_input'][$field_name] = $form_state_value;
            foreach ($form_state_value as $key => $value) {
              if (!is_numeric($key)) {
                unset($form_state_values['default_value_input'][$field_name][$key]);
              }
              elseif (isset($form['default_value_input'][$field_name]['widget'][$key]['#paragraph_type'])) {
                $form_state_values['default_value_input'][$field_name][$key]['paragraph_type'] = $form['default_value_input'][$field_name]['widget'][$key]['#paragraph_type'];
              }
            }
          }
        }
        if (isset($this->userInput['fields']) && isset($form_state_values['default_value_input'])) {
          $this->userInput['fields'] = array_merge($this->userInput['fields'], $form_state_values['default_value_input']);
        }
        $form_state
          ->setRebuild();
        break;
      case 3:
        if (method_exists($this, 'updateFields')) {
          $return_verify = $this
            ->updateFields();
        }
        $this
          ->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('Bulk Update Fields');
    $submit_label = 'Next';
    switch ($this->step) {
      case 1:

        // Retrieve IDs from the temporary storage.
        $this->userInput['entities'] = $this->tempStoreFactory
          ->get('bulk_update_fields_ids')
          ->get($this->currentUser
          ->id());
        $options = [];

        // Exclude some base fields.
        // TODO add date fields and revision log.
        $excluded_base_fields = [
          'nid',
          'uuid',
          'vid',
          'type',
          'revision_uid',
          'title',
          'path',
          'menu_link',
          'status',
          'uid',
          'default_langcode',
          'revision_timestamp',
          'revision_log',
          'created',
          'changed',
          'pass',
          'name',
          'mail',
          'init',
        ];
        $excluded_fields = $this
          ->config('bulk_update_fields.settings')
          ->get('exclude');

        // Make it possible to bulk update 'Generate automatic URL alias'.
        // @todo: add code to remove 'URL alias'.
        if (\Drupal::moduleHandler()
          ->moduleExists('pathauto')) {
          if (($key = array_search('path', $excluded_base_fields)) !== FALSE) {
            unset($excluded_base_fields[$key]);
          }
        }
        foreach ($this->userInput['entities'] as $index => $entity) {
          $langcode = explode(':', $index)[1];
          $entity = $entity
            ->getTranslation($langcode);
          $this->entity = $entity;
          $fields = $entity
            ->getFieldDefinitions();
          foreach ($fields as $field) {
            if (!in_array($field
              ->getName(), $excluded_base_fields) && !isset($options[$field
              ->getName()])) {
              if (!in_array($field
                ->getName(), array_filter($excluded_fields))) {
                $options[$field
                  ->getName()]['field_name'] = $field
                  ->getLabel() ? $field
                  ->getLabel() : $field
                  ->getName();
              }
            }
          }
        }
        $header = [
          'field_name' => $this
            ->t('Field Name'),
        ];
        $form['#title'] .= ' - ' . $this
          ->t('Select Fields to Alter');
        $form['table'] = [
          '#type' => 'tableselect',
          '#header' => $header,
          '#options' => $options,
          '#empty' => $this
            ->t('No fields found'),
        ];
        break;
      case 2:
        foreach ($this->userInput['entities'] as $index => $entity) {
          $langcode = explode(':', $index)[1];
          $entity = $entity
            ->getTranslation($langcode);
          $this->entity = $entity;
          foreach ($this->userInput['fields'] as $field_name) {
            $temp_form_element = [];
            $temp_form_state = new FormState();
            $temp_form_state
              ->setFormObject($form_state
              ->getFormObject());
            if ($field = $entity
              ->getFieldDefinition($field_name)) {

              // TODO Dates fields are incorrect due to TODOs below.
              if ($field
                ->getType() == 'datetime') {
                $type = \Drupal::service('plugin.manager.field.widget');
                $plugin_definition = $type
                  ->getDefinition('datetime_default');
                $widget = new DateTimeWidgetBase('datetime', $plugin_definition, $entity
                  ->get($field_name)
                  ->getFieldDefinition(), [], []);
                $form['#parents'] = [];
                $form['default_value_input'][$field_name] = $widget
                  ->form($entity
                  ->get($field_name), $form, $form_state);
              }
              elseif ($field
                ->getType() == 'entity_reference_revisions') {

                // TODO - allow other types of entity_reference_revisions
                // currently paragraphs only.
                $type = \Drupal::service('plugin.manager.field.widget');
                $plugin_definition = $type
                  ->getDefinition('paragraphs');
                $widget = new ParagraphsWidget('paragraphs', $plugin_definition, $entity
                  ->get($field_name)
                  ->getFieldDefinition(), [], []);
                $form['#parents'] = [];
                $form['default_value_input'][$field_name] = $widget
                  ->form($entity
                  ->get($field_name), $form, $form_state);
                if ($form_state
                  ->getTriggeringElement()['#name'] != 'op') {
                  $paragraph_type = $form_state
                    ->getTriggeringElement()['#bundle_machine_name'];
                  $form_state
                    ->set('paragraph_type', $paragraph_type);
                }
              }
              else {

                // TODO
                // I cannot figure out how to get a form element for only a
                // field. Maybe someone else can.
                // TODO Doing it this way does not allow for feild labels on
                // textarea widgets.
                $form[$field_name] = $entity
                  ->get($field_name)
                  ->defaultValuesForm($temp_form_element, $temp_form_state);
              }
            }
          }
        }
        $form['#title'] .= ' - ' . $this
          ->t('Enter New Values in Appropriate Fields');
        break;
      case 3:
        $form['#title'] .= ' - ' . $this
          ->t('Are you sure you want to alter @count_fields fields on @count_entities entities?', [
          '@count_fields' => count($this->userInput['fields']),
          '@count_entities' => count($this->userInput['entities']),
        ]);
        $submit_label = 'Update Fields';
        break;
    }
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $submit_label,
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // TODO.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BulkUpdateFieldsForm::$currentUser private property User.
BulkUpdateFieldsForm::$routeBuilder protected property The route builder.
BulkUpdateFieldsForm::$sessionManager private property Session.
BulkUpdateFieldsForm::$step protected property Set a var to make stepthrough form.
BulkUpdateFieldsForm::$tempStoreFactory protected property Tempstorage.
BulkUpdateFieldsForm::$userInput protected property Keep track of user input.
BulkUpdateFieldsForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
BulkUpdateFieldsForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
BulkUpdateFieldsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
BulkUpdateFieldsForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
BulkUpdateFieldsForm::updateFields public function
BulkUpdateFieldsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
BulkUpdateFieldsForm::__construct public function Constructs a \Drupal\bulk_update_fields\Form\BulkUpdateFieldsForm.
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::$routeMatch protected property The route match.
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.