You are here

class BulkEditForm in Views Bulk Edit 8

Same name and namespace in other branches
  1. 8.2 src/Form/BulkEditForm.php \Drupal\views_bulk_edit\Form\BulkEditForm

The bulk edit form.

Hierarchy

Expanded class hierarchy of BulkEditForm

1 string reference to 'BulkEditForm'
views_bulk_edit.routing.yml in ./views_bulk_edit.routing.yml
views_bulk_edit.routing.yml

File

src/Form/BulkEditForm.php, line 16

Namespace

Drupal\views_bulk_edit\Form
View source
class BulkEditForm extends FormBase {

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

  /**
   * Private temp store factory.
   *
   * @var \Drupal\user\PrivateTempStoreFactory
   */
  protected $privateTempStoreFactory;

  /**
   * BulkEditForm constructor.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, PrivateTempStoreFactory $privateTempStoreFactory) {
    $this->entityTypeManager = $entityTypeManager;
    $this->privateTempStoreFactory = $privateTempStoreFactory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('user.private_tempstore'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    if (!$this
      ->getBulkEditEntityData()) {
      $form['direct_access']['#markup'] = $this
        ->t('You must use a valid bulk operations form to first select the entities to change');
      return $form;
    }
    $form['#attributes']['class'] = [
      'bulk-edit-form',
    ];
    $form["#attached"]['library'][] = 'views_bulk_edit/views_bulk_edit.edit_form';
    $form['#tree'] = TRUE;
    $form['selector'] = [
      '#type' => 'container',
      '#tree' => TRUE,
    ];
    foreach ($this
      ->getBulkEditEntityData() as $entity_type_id => $bundle_entities) {
      foreach ($bundle_entities as $bundle => $entities) {
        $form = $this
          ->getForm($entity_type_id, $bundle, $form, $form_state);
      }
    }
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
      '#submit' => [
        '::submitForm',
      ],
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * Gets the form for this entity display.
   */
  protected function getForm($entity_type_id, $bundle, $form, FormStateInterface $form_state) {
    $form[$bundle] = [
      '#type' => 'fieldset',
      '#tree' => TRUE,
      '#parents' => [
        $bundle,
      ],
      '#title' => $bundle,
    ];
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $entity = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->create([
      $entity_type
        ->getKey('bundle') => $bundle,
    ]);
    $form_display = EntityFormDisplay::collectRenderDisplay($entity, 'bulk_edit');
    $form_display
      ->buildForm($entity, $form[$bundle], $form_state);
    $form_state
      ->set('form_display', $form_display);
    $form[$bundle] += $this
      ->getSelectorForm($bundle, $form[$bundle]);
    return $form;
  }

  /**
   * Builds the selector form.
   *
   * Given an entity form, create a selector form to provide options to update
   * values.
   *
   * @param string $bundle
   *   The bundle machine name.
   * @param array $form
   *   The form we're building the selection options for.
   *
   * @return array
   *   The new selector form.
   */
  protected function getSelectorForm($bundle, array &$form) {
    $selector['field_selector'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Select fields to change'),
      '#weight' => -50,
      '#tree' => TRUE,
    ];
    foreach (Element::children($form) as $key) {
      if ($key == 'field_selector' || !($element =& $this
        ->findFormElement($form[$key]))) {
        continue;
      }
      $element['#required'] = FALSE;
      $element['#tree'] = TRUE;

      // Add the toggle field to the form.
      $selector['field_selector'][$key] = [
        '#type' => 'checkbox',
        '#title' => $element['#title'],
        '#tree' => TRUE,
      ];

      // Force the original value to be hidden unless the checkbox is enabled.
      $form[$key]['#states'] = [
        'visible' => [
          sprintf('[name="%s[field_selector][%s]"]', $bundle, $key) => [
            'checked' => TRUE,
          ],
        ],
      ];
    }
    return $selector;
  }

  /**
   * Finds the deepest most form element and returns it.
   *
   * @param array $form
   *   The form element we're searching.
   *
   * @return array|null
   *   The deepest most element if we can find it.
   */
  protected function &findFormElement(array &$form) {
    foreach (Element::children($form) as $key) {
      if (isset($form[$key]['#title']) && isset($form[$key]['#type'])) {
        return $form[$key];
      }
      elseif (is_array($form[$key])) {
        $element =& $this
          ->findFormElement($form[$key]);
        return $element;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entities_by_bundle = $this
      ->getEntitiesByBundle();
    $count = 0;
    $form_state
      ->cleanValues();
    foreach ($entities_by_bundle as $bundle => $entities) {
      $update_values = array_filter($form_state
        ->getValue([
        $bundle,
        'field_selector',
      ]));
      $count += count($entities);
      foreach ($entities as $entity) {
        $form['#parents'] = [
          $bundle,
        ];
        $form_state
          ->setValue($bundle, $this
          ->filterOnKey($form_state
          ->getValue($bundle), function ($key) use ($update_values) {
          return !empty($update_values[$key]);
        }));
        $form_state
          ->get('form_display')
          ->extractFormValues($entity, $form, $form_state);
        $entity
          ->save();
      }
    }
    drupal_set_message(t('Updated @count entities', [
      '@count' => $count,
    ]));
    $this
      ->clearBulkEditEntityData();
  }

  /**
   * Provides same functionality as ARRAY_FILTER_USE_KEY for PHP 5.5.
   *
   * @param array $array
   *   The array of data to filter.
   * @param callable $callback
   *   The function we're going to use to determine the filtering.
   *
   * @return array
   *   The filtered data.
   */
  protected function filterOnKey(array $array, callable $callback) {
    $filtered_values = [];
    foreach ($array as $key => $value) {
      if ($callback($key)) {
        $filtered_values[$key] = $value;
      }
    }
    return $filtered_values;
  }

  /**
   * Gets the saved entity data.
   *
   * @return array
   *   An array of saved entity data.
   */
  protected function getBulkEditEntityData() {
    $privateStore = $this->privateTempStoreFactory
      ->get('views_bulk_edit');
    return $privateStore
      ->get('entity_data') ?: [];
  }

  /**
   * Clear the saved entities once we've finished with them.
   */
  protected function clearBulkEditEntityData() {
    $this->privateTempStoreFactory
      ->get('views_bulk_edit')
      ->delete('entity_data');
  }

  /**
   * Gets the loaded entities.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]
   *   An array of loaded entities.
   */
  protected function getEntitiesByBundle() {
    $entities_by_bundle = [];
    foreach ($this
      ->getBulkEditEntityData() as $entity_type_id => $bundle_entity_ids) {
      foreach ($bundle_entity_ids as $bundle => $entity_ids) {
        $entities_by_bundle[$bundle] = $this->entityTypeManager
          ->getStorage($entity_type_id)
          ->loadMultiple($entity_ids);
      }
    }
    return $entities_by_bundle;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BulkEditForm::$entityTypeManager protected property The entity type manager.
BulkEditForm::$privateTempStoreFactory protected property Private temp store factory.
BulkEditForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
BulkEditForm::clearBulkEditEntityData protected function Clear the saved entities once we've finished with them.
BulkEditForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
BulkEditForm::filterOnKey protected function Provides same functionality as ARRAY_FILTER_USE_KEY for PHP 5.5.
BulkEditForm::findFormElement protected function Finds the deepest most form element and returns it.
BulkEditForm::getBulkEditEntityData protected function Gets the saved entity data.
BulkEditForm::getEntitiesByBundle protected function Gets the loaded entities.
BulkEditForm::getForm protected function Gets the form for this entity display.
BulkEditForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
BulkEditForm::getSelectorForm protected function Builds the selector form.
BulkEditForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
BulkEditForm::__construct public function BulkEditForm constructor.
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.