/**
  * {@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;
 }
Example #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();
 }
Example #3
0
 /**
  * Test getAllAttributeMetadata
  */
 public function testGetAllAttributeMetadata()
 {
     $data = ['attribute_id' => 1, 'attribute_code' => 'description', 'frontend_label' => 'English', 'store_labels' => array(1 => 'France'), 'frontend_input' => 'textarea'];
     $attributeMock = $this->getMock('Magento\\Framework\\Object', array('usesSource', 'getSource', 'isScopeGlobal', 'getId'), array('data' => $data));
     $attributeMock->expects($this->once())->method('getId')->will($this->returnValue($data['attribute_id']));
     $attributeMock->expects($this->any())->method('isScopeGlobal')->will($this->returnValue(true));
     $attributeMock->expects($this->any())->method('usesSource')->will($this->returnValue(true));
     $attributeMock->expects($this->any())->method('getSource')->will($this->returnValue(new \Magento\Framework\Object()));
     $this->eavConfigMock->expects($this->any())->method('getAttribute')->will($this->returnValue($attributeMock));
     $this->attributeCollectionFactory->expects($this->once())->method('create')->will($this->returnValue($this->attributeCollection));
     $searchCriteria = $this->getMockBuilder('\\Magento\\Framework\\Service\\V1\\Data\\SearchCriteria')->disableOriginalConstructor()->setMethods(['getFilterGroups'])->getMock();
     $searchCriteria->expects($this->once())->method('getFilterGroups')->will($this->returnValue(array()));
     $this->attributeCollection->expects($this->once())->method('getSize')->will($this->returnValue(1));
     $this->attributeCollection->expects($this->once())->method('getIterator')->will($this->returnValue(new \ArrayIterator(array($attributeMock))));
     /** @var \Magento\Catalog\Service\V1\Data\Product\Attribute\SearchResults $searchResult */
     $searchResult = $this->service->getAllAttributeMetadata('entity_type', $searchCriteria);
     $dto = $searchResult->getItems()[0];
     $this->assertInstanceOf('Magento\\Framework\\Service\\Data\\AbstractExtensibleObject', $dto);
     $this->assertEquals($attributeMock->getFrontendInput(), $dto->getFrontendInput());
     $this->assertEquals(0, $dto->getFrontendLabel()[0]->getStoreId());
     $this->assertEquals(1, $dto->getFrontendLabel()[1]->getStoreId());
     $this->assertEquals('English', $dto->getFrontendLabel()[0]->getLabel());
     $this->assertEquals('France', $dto->getFrontendLabel()[1]->getLabel());
 }