Score:0

Bulk update product variations weight

th flag

I've created a module for a custom VBO action. The action can be selected in the view, and the function is executed, but $request is empty and the selected product variation IDs are not available.

class VariationWeightSet extends ViewsBulkOperationsActionBase implements PluginFormInterface {

  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $request = \Drupal::request();
    $storage = \Drupal::service('entity_type.manager')->getStorage('commerce_product_variation');

    if ($ids = explode('|', $request->query->get('ids'))) {
      $variations = $storage->loadMultiple($ids);
      $form_state->set('variations', array_values($variations));
      $variation = end($variations);
      $weight = $variation->getWeight()->toArray();
      $form['warning'] = [
        '#markup' => new TranslatableMarkup('<h1>Set new Weight for <span style="color:red">@count</span> variations</h1>', ['@count' => count($variations)]),
      ];
      $form['weight'] = [
        '#type' => 'container',
        '#attributes' => ['class' => ['container-inline']],
      ];
    }
    return $form;
  }

}

How can I get the selected product variation IDs?

apaderno avatar
us flag
Unfortunately, we don't write code on demand. We can answer questions about code that doesn't work as expected, though, as long as the answer is Drupal-specific.
user3086053 avatar
th flag
Yeah, I understand completely. I will start coding and try to update the question within a few days. Thanks.
user3086053 avatar
th flag
The request is empty after submitting the VBO form.
apaderno avatar
us flag
The purpose of `buildConfigurationForm()` is building the form used to configure the action. The submitted values aren't taken from `\Drupal::request()`.
user3086053 avatar
th flag
I understand. In case I want to add a #default_value?
apaderno avatar
us flag
You should first read [Views Bulk Operations (VBO)](https://www.drupal.org/docs/contributed-modules/views-bulk-operations-vbo).
user3086053 avatar
th flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/139931/discussion-between-user3086053-and-apaderno).
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.