/**
  * {@inheritdoc}
  */
 public function getList($entityTypeCode, \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
 {
     if (!$entityTypeCode) {
         throw InputException::requiredField('entity_type_code');
     }
     /** @var \Magento\Eav\Model\Resource\Entity\Attribute\Collection $attributeCollection */
     $attributeCollection = $this->attributeCollectionFactory->create();
     $attributeCollection->addFieldToFilter('entity_type_code', ['eq' => $entityTypeCode]);
     $attributeCollection->join(['entity_type' => $attributeCollection->getTable('eav_entity_type')], 'main_table.entity_type_id = entity_type.entity_type_id', []);
     $attributeCollection->join(['eav_entity_attribute' => $attributeCollection->getTable('eav_entity_attribute')], 'main_table.attribute_id = eav_entity_attribute.attribute_id', []);
     $attributeCollection->join(['additional_table' => $attributeCollection->getTable('catalog_eav_attribute')], 'main_table.attribute_id = additional_table.attribute_id', []);
     //Add filters from root filter group to the collection
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $attributeCollection);
     }
     /** @var \Magento\Framework\Api\SortOrder $sortOrder */
     foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) {
         $attributeCollection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SearchCriteriaInterface::SORT_ASC ? 'ASC' : 'DESC');
     }
     $totalCount = $attributeCollection->getSize();
     // Group attributes by id to prevent duplicates with different attribute sets
     $attributeCollection->addAttributeGrouping();
     $attributeCollection->setCurPage($searchCriteria->getCurrentPage());
     $attributeCollection->setPageSize($searchCriteria->getPageSize());
     $attributes = [];
     /** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
     foreach ($attributeCollection as $attribute) {
         $attributes[] = $this->get($entityTypeCode, $attribute->getAttributeCode());
     }
     $searchResults = $this->searchResultsFactory->create();
     $searchResults->setSearchCriteria($searchCriteria);
     $searchResults->setItems($attributes);
     $searchResults->setTotalCount($totalCount);
     return $searchResults;
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  */
 public function getAllAttributeMetadata($entityType, SearchCriteria $searchCriteria)
 {
     $this->searchResultsBuilder->setSearchCriteria($searchCriteria);
     /** @var \Magento\Eav\Model\Resource\Entity\Attribute\Collection $attributeCollection */
     $attributeCollection = $this->attributeCollectionFactory->create();
     $attributeCollection->join(array('entity_type' => $attributeCollection->getTable('eav_entity_type')), 'main_table.entity_type_id = entity_type.entity_type_id', []);
     $attributeCollection->addFieldToFilter('entity_type_code', ['eq' => $entityType]);
     $attributeCollection->join(['eav_entity_attribute' => $attributeCollection->getTable('eav_entity_attribute')], 'main_table.attribute_id = eav_entity_attribute.attribute_id', []);
     $attributeCollection->join(array('additional_table' => $attributeCollection->getTable('catalog_eav_attribute')), 'main_table.attribute_id = additional_table.attribute_id', []);
     //Add filters from root filter group to the collection
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $attributeCollection);
     }
     /** @var SortOrder $sortOrder */
     foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) {
         $attributeCollection->addOrder($this->translateField($sortOrder->getField()), $sortOrder->getDirection() == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC');
     }
     $totalCount = $attributeCollection->getSize();
     // Group attributes by id to prevent duplicates with different attribute sets
     $attributeCollection->addAttributeGrouping();
     $attributeCollection->setCurPage($searchCriteria->getCurrentPage());
     $attributeCollection->setPageSize($searchCriteria->getPageSize());
     $attributes = [];
     /** @var \Magento\Eav\Model\Entity\Attribute $attribute */
     foreach ($attributeCollection as $attribute) {
         $attributes[] = $this->getAttributeMetadata($entityType, $attribute->getAttributeCode());
     }
     $this->searchResultsBuilder->setItems($attributes);
     $this->searchResultsBuilder->setTotalCount($totalCount);
     return $this->searchResultsBuilder->create();
 }