Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinkTypeProvider.php
Go to the documentation of this file.
1 <?php
9 
11 {
19  protected $linkTypes;
20 
24  protected $linkTypeFactory;
25 
30 
34  protected $linkFactory;
35 
42  public function __construct(
43  \Magento\Catalog\Api\Data\ProductLinkTypeInterfaceFactory $linkTypeFactory,
44  \Magento\Catalog\Api\Data\ProductLinkAttributeInterfaceFactory $linkAttributeFactory,
45  \Magento\Catalog\Model\Product\LinkFactory $linkFactory,
46  array $linkTypes = []
47  ) {
48  $this->linkTypes = $linkTypes;
49  $this->linkTypeFactory = $linkTypeFactory;
50  $this->linkAttributeFactory = $linkAttributeFactory;
51  $this->linkFactory = $linkFactory;
52  }
53 
59  public function getLinkTypes()
60  {
61  return $this->linkTypes;
62  }
63 
67  public function getItems()
68  {
69  $output = [];
70  foreach ($this->getLinkTypes() as $type => $typeCode) {
72  $linkType = $this->linkTypeFactory->create();
73  $linkType->setName($type)
74  ->setCode($typeCode);
75  $output[] = $linkType;
76  }
77  return $output;
78  }
79 
83  public function getItemAttributes($type)
84  {
85  $output = [];
86  $types = $this->getLinkTypes();
87  $typeId = isset($types[$type]) ? $types[$type] : null;
88 
90  $link = $this->linkFactory->create(['data' => ['link_type_id' => $typeId]]);
91  $attributes = $link->getAttributes();
92  foreach ($attributes as $item) {
94  $linkAttribute = $this->linkAttributeFactory->create();
95  $linkAttribute->setCode($item['code'])
96  ->setType($item['type']);
97  $output[] = $linkAttribute;
98  }
99  return $output;
100  }
101 }
$type
Definition: item.phtml:13
$attributes
Definition: matrix.phtml:13