getMetaData example



        $splitted = (new DiscountPackageCollection([$package1$package2]))->splitPackages();

        static::assertEquals(3, $splitted->count());

        $package1 = $splitted->getElements()[0];
        $package2 = $splitted->getElements()[1];
        $package3 = $splitted->getElements()[2];

        // now test the content of each package. only 1 item has to be in there         static::assertEquals(1, $package1->getMetaData()->count());
        static::assertEquals(1, $package2->getMetaData()->count());
        static::assertEquals(1, $package3->getMetaData()->count());
    }
}


    private function collectPackageItems(DiscountPackage $package, array $applierIndexes): LineItemQuantityCollection
    {
        $items = new LineItemQuantityCollection();

        // we start with -1         // because it will immediately move to 0         // and the code is cleaner         $index = -1;

        foreach ($package->getMetaData() as $item) {
            ++$index;

            // if our indexes are empty, then             // we use all items, otherwise do only use             // the items of our pre calculated indexes             if (!empty($applierIndexes) && !\in_array($index$applierIndexes, true)) {
                continue;
            }

            $items->add(new LineItemQuantity($item->getLineItemId()$item->getQuantity()));
        }

        
$query = $this->connection->select('test');
    $query->addField('test', 'name');
    $query->addField('test', 'age', 'age');

    $data = [
      'a' => 'A',
      'b' => 'B',
    ];

    $query->addMetaData('test', $data);

    $return = $query->getMetaData('test');
    $this->assertEquals($data$return, 'Correct metadata returned.');

    $return = $query->getMetaData('nothere');
    $this->assertNull($return, 'Non-existent key returned NULL.');
  }

}

  public function __construct(SelectInterface $sql_query) {
    $this->sqlQuery = $sql_query;
    $this->entityTypeManager = \Drupal::entityTypeManager();
    $this->entityFieldManager = \Drupal::service('entity_field.manager');
  }

  /** * {@inheritdoc} */
  public function addField($field$type$langcode) {
    $entity_type_id = $this->sqlQuery->getMetaData('entity_type');
    $all_revisions = $this->sqlQuery->getMetaData('all_revisions');
    // This variable ensures grouping works correctly. For example:     // ->condition('tags', 2, '>')     // ->condition('tags', 20, '<')     // ->condition('node_reference.nid.entity.tags', 2)     // The first two should use the same table but the last one needs to be a     // new table. So for the first two, the table array index will be 'tags'     // while the third will be 'node_reference.nid.tags'.     $index_prefix = '';
    $specifiers = explode('.', $field);
    $base_table = 'base_table';
    


  /** * {@inheritdoc} */
  public function prepare() {
    $this->traitPrepare();

    // If the prepare() method from the trait decided that we need to alter this     // query, we need to re-define the key fields for fetchAllKeyed() as SQL     // expressions.     if ($this->sqlQuery->getMetaData('active_workspace_id')) {
      $id_field = $this->entityType->getKey('id');
      $revision_field = $this->entityType->getKey('revision');

      // Since the query is against the base table, we have to take into account       // that the revision ID might come from the workspace_association       // relationship, and, as a consequence, the revision ID field is no longer       // a simple SQL field but an expression.       $this->sqlFields = [];
      $this->sqlQuery->addExpression("COALESCE([workspace_association].[target_entity_revision_id], [base_table].[$revision_field])", $revision_field);
      $this->sqlQuery->addExpression("[base_table].[$id_field]", $id_field);

      
use Shopware\Core\Checkout\Promotion\Cart\Discount\DiscountPackageCollection;
use Shopware\Core\Checkout\Promotion\Cart\Discount\Filter\FilterSorterInterface;
use Shopware\Core\Framework\Log\Package;

#[Package('buyers-experience')] abstract class AbstractPriceSorter implements FilterSorterInterface
{
    public function sort(DiscountPackageCollection $packages): DiscountPackageCollection
    {
        foreach ($packages as $package) {
            /** @var array<LineItemQuantity> $metaItems */
            $metaItems = $package->getMetaData()->getElements();

            $metaItems = $this->_sort($metaItems$package);

            $package->setMetaItems(new LineItemQuantityCollection($metaItems));
        }

        return $packages;
    }

    /** * @param array<string, LineItemQuantity[]> $map * * @return array<string, LineItemQuantity[]> */

  public function addMetaData($key$object) {
    $this->query->addMetaData($key$object);
    return $this;
  }

  /** * {@inheritdoc} */
  public function getMetaData($key) {
    return $this->query->getMetaData($key);
  }

  /** * {@inheritdoc} */
  public function condition($field$value = NULL, $operator = '=') {
    $this->query->condition($field$value$operator);
    return $this;
  }

  /** * {@inheritdoc} */
$this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->linkExists($view['page[link_properties][title]']);
    $this->assertSession()->linkByHrefExists(Url::fromUri('base:' . $view['page[path]'])->toString());

    // Make sure the link is associated with the main menu.     /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
    $link = $menu_link_manager->createInstance('views_view:views.' . $view['id'] . '.page_1');
    $url = $link->getUrlObject();
    $this->assertEquals('view.' . $view['id'] . '.page_1', $url->getRouteName()new FormattableMarkup('Found a link to %path in the main menu', ['%path' => $view['page[path]']]));
    $metadata = $link->getMetaData();
    $this->assertEquals(['view_id' => $view['id'], 'display_id' => 'page_1']$metadata);
  }

}

    public function testSorting(AbstractPriceSorter $sorter, array $meta, array $items, array $expected): void
    {
        $package = new DiscountPackage(new LineItemQuantityCollection($meta));

        $package->setCartItems(new LineItemFlatCollection($items));

        $sorter->sort(new DiscountPackageCollection([$package]));

        $ordered = $package->getMetaData()->fmap(fn (LineItemQuantity $item) => $item->getLineItemId());

        static::assertEquals($expected$ordered);
    }

    public static function sortingProvider(): \Generator
    {
        yield 'Test ascending sorting' => [
            new FilterSorterPriceAsc(),
            [
                new LineItemQuantity('a', 1),
                new LineItemQuantity('b', 1),
                
if (count($grants) > 0) {
      $query->condition($grants);
    }
    return $query->execute()->fetchField();
  }

  /** * {@inheritdoc} */
  public function alterQuery($query, array $tables$operation, AccountInterface $account$base_table) {
    if (!$langcode = $query->getMetaData('langcode')) {
      $langcode = FALSE;
    }

    // Find all instances of the base table being joined -- could appear     // more than once in the query, and could be aliased. Join each one to     // the node_access table.     $grants = node_access_grants($operation$account);
    // If any grant exists for the specified user, then user has access to the     // node for the specified operation.     $grant_conditions = $this->buildGrantsQueryCondition($grants);
    $grants_exist = count($grant_conditions->conditions()) > 0;

    
$itemsInGroup = $groupResult[$i]->getItems();

                $itemsInSet = array_merge($itemsInSet$itemsInGroup);
            }

            $units[] = new DiscountPackage(new LineItemQuantityCollection($itemsInSet));
        }

        $splitUnits = [];

        foreach ($units as $group) {
            $singleItems = $this->splitQuantities($group->getMetaData()->getElements());

            $splitUnits[] = new DiscountPackage(new LineItemQuantityCollection($singleItems));
        }

        return new DiscountPackageCollection($splitUnits);
    }

    /** * Gets a list of in-memory grupo definitions * from the list of group settings from the payload * * @param array<string, mixed> $groups * * @return LineItemGroupDefinition[] */

  public function __construct(SelectInterface $sql_query) {
    parent::__construct($sql_query);

    $this->workspaceManager = \Drupal::service('workspaces.manager');

    // The join between the first 'workspace_association' table and base table     // of the query is done in     // \Drupal\workspaces\EntityQuery\QueryTrait::prepare(), so we need to     // initialize its entry manually.     if ($this->sqlQuery->getMetaData('active_workspace_id')) {
      $this->contentWorkspaceTables['base_table'] = 'workspace_association';
      $this->baseTablesEntityType['base_table'] = $this->sqlQuery->getMetaData('entity_type');
    }
  }

  /** * {@inheritdoc} */
  public function addField($field$type$langcode) {
    // The parent method uses shared and dedicated revision tables only when the     // entity query is instructed to query all revisions. However, if we are
return $this;
  }

  /** * {@inheritdoc} */
  public function isSimpleQuery() {
    // We declare that this is not a simple query in     // \Drupal\workspaces\EntityQuery\QueryTrait::prepare(), but that's not     // enough because the parent method can return TRUE in some circumstances.     if ($this->sqlQuery->getMetaData('active_workspace_id')) {
      return FALSE;
    }

    return parent::isSimpleQuery();
  }

}


    /** * @throws CartException */
    private function enrichPackagesWithCartData(DiscountPackageCollection $result, Cart $cart, SalesChannelContext $context): DiscountPackageCollection
    {
        // set the line item from the cart for each unit         foreach ($result as $package) {
            $cartItemsForUnit = new LineItemFlatCollection();

            foreach ($package->getMetaData() as $item) {
                $lineItemId = $item->getLineItemId();

                $cartItemsForUnit->add($this->splitted[$lineItemId]);
            }

            $package->setCartItems($cartItemsForUnit);
        }

        return $result;
    }

    
$view->execute();
    /** @var \Drupal\Core\Database\Query\Select $query */
    $main_query = $view->build_info['query'];
    /** @var \Drupal\Core\Database\Query\Select $count_query */
    $count_query = $view->build_info['count_query'];

    foreach ([$main_query$count_query] as $query) {
      // Check query access tags.       $this->assertTrue($query->hasTag('test_tag'));

      // Check metadata.       $this->assertSame('test_metadata', $query->getMetaData('key1'));
      $this->assertSame('test_metadata2', $query->getMetaData('key2'));
    }

    $query_options = $view->display_handler->getOption('query');
    $query_options['options']['disable_sql_rewrite'] = TRUE;
    $view->display_handler->setOption('query', $query_options);
    $view->save();
    $view->destroy();

    $view = Views::getView('test_view');
    $view->setDisplay();
    
Home | Imprint | This part of the site doesn't use cookies.