### Magento 2 Installation Commands for Custom Entity Product Link Module
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
Provides the necessary command-line instructions to install and enable the Custom Entity Product Link module in a Magento 2 environment. This includes using Composer to require the module and running Magento's setup commands to apply database schema changes, compile code, and deploy static content.
```bash
# Install via Composer
composer require smile/module-custom-entity-product-link
# Enable module and run setup
bin/magento module:enable Smile_CustomEntityProductLink
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy
bin/magento cache:flush
# Reindex if necessary
bin/magento indexer:reindex
```
--------------------------------
### Install Magento2 Module using Composer
Source: https://github.com/smile-sa/magento2-module-custom-entity-product-link/wiki/GettingStarted
Installs the 'smile/module-custom-entity-product-link' module into your Magento2 directory using Composer. After composer install, it's necessary to run Magento's setup upgrade command to finalize the installation.
```bash
composer install smile/module-custom-entity-product-link
bin/magento setup:upgrade
```
--------------------------------
### Retrieve Custom Entities Linked to a Product
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
Fetches and displays custom entities associated with a specific product using the CustomEntityProductLinkManagementInterface. Requires the ProductRepositoryInterface to get the product by ID. Outputs the name of each linked entity grouped by its attribute.
```php
linkManagement = $linkManagement;
$this->productRepository = $productRepository;
}
public function getProductCustomEntities(int $productId): ?array
{
try {
$product = $this->productRepository->getById($productId);
// Returns array indexed by attribute code, each containing CustomEntityInterface objects
$customEntities = $this->linkManagement->getCustomEntities($product);
if ($customEntities) {
foreach ($customEntities as $attributeCode => $entities) {
foreach ($entities as $entity) {
echo "Attribute: {$attributeCode}, Entity: {$entity->getName()}\n";
}
}
}
return $customEntities;
} catch (\Exception $e) {
// Handle exception
return null;
}
}
}
```
--------------------------------
### Get Filterable Attribute Code for Custom Entity (PHP)
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
Generates a filter URL for a custom entity by retrieving its filterable attribute code. It handles exceptions during entity retrieval and attribute code fetching. Dependencies include the module's helper class and the custom entity repository.
```php
productHelper = $productHelper;
$this->entityRepository = $entityRepository;
}
public function getFilterUrlForEntity(int $entityId): ?string
{
try {
$entity = $this->entityRepository->get($entityId);
// Returns the attribute code that can be used for filtering by this entity
$attributeCode = $this->productHelper->getFilterableAttributeCode($entity);
if ($attributeCode) {
// Build filter URL
return "?{$attributeCode}={$entityId}";
}
return null;
} catch (\Exception $e) {
return null;
}
}
}
```
--------------------------------
### Dependency Injection Configuration for Magento 2
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
This XML configuration sets up dependency injection for the custom entity product link module in Magento 2. It defines interface implementations, extension pool actions for product data reading, and custom layer resolvers for specific entity pages.
```xml
-
-
-
Smile\CustomEntityProductLink\Model\Product\CustomEntity\ReadHandler
-
Smile\CustomEntityProductLink\Model\Layer\CustomEntity
```
--------------------------------
### Composer Configuration for Custom Entity Product Link Module
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
Defines the metadata for the 'smile/module-custom-entity-product-link' Magento 2 module. It specifies dependencies on PHP versions, other Smile modules, and ElasticSuite, along with autoloading configurations for the module's code.
```json
{
"name": "smile/module-custom-entity-product-link",
"type": "magento2-module",
"description": "Smile - Custom Entity Product link Module",
"keywords": ["magento2", "custom", "entity", "link", "product"],
"license": "OSL-3.0",
"require": {
"php": "^7.4 || ^8.1",
"smile/module-custom-entity": "^1.3",
"smile/elasticsuite": "^2.8.0"
},
"autoload": {
"files": ["registration.php"],
"psr-4": {
"Smile\\CustomEntityProductLink\\": ""
}
}
}
```
--------------------------------
### List Product Attributes for Custom Entities
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
Retrieves all product attributes configured with the 'smile_custom_entity' frontend input type using the SmileCustomEntityProductLinkHelperData class. It returns an array containing the code, label, and entity set ID for each matching attribute.
```php
helper = $helper;
}
public function listCustomEntityAttributes(): array
{
// Returns all product attributes with frontend_input = 'smile_custom_entity'
$attributes = $this->helper->getCustomEntityProductAttributes();
$result = [];
foreach ($attributes as $attribute) {
$result[] = [
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getDefaultFrontendLabel(),
'entity_set_id' => $attribute->getCustomEntityAttributeSetId()
];
}
return $result;
}
}
```
--------------------------------
### Register Magento 2 Module
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
Registers the 'Smile_CustomEntityProductLink' module within the Magento 2 application. This is a standard procedure for enabling any Magento module.
```php
productHelper = $productHelper;
$this->productRepository = $productRepository;
}
public function getEntitiesByAttribute(int $productId, string $attributeCode): array
{
$product = $this->productRepository->getById($productId);
// Get custom entities for specific attribute from extension attributes
$entities = $this->productHelper->getCustomEntities($product, $attributeCode);
$result = [];
foreach ($entities as $entity) {
if ($entity->getIsActive()) {
$result[] = [
'id' => $entity->getId(),
'name' => $entity->getName(),
'url_key' => $entity->getUrlKey(),
'attribute_set_id' => $entity->getAttributeSetId()
];
}
}
return $result;
}
}
```
--------------------------------
### Extension Attributes Configuration (XML)
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
Defines an extension attribute named 'custom_entities' for the Magento Product interface. This attribute is typed as an array of `SmileCustomEntityApiDataCustomEntityInterface`, enabling the association of custom entities with products.
```xml
```
--------------------------------
### Product Plugin for Cache Identity Management (PHP)
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
This PHP plugin intercepts the `getIdentities` method of the Magento Product model to add custom entity cache tags. It checks for changes in custom entity product attributes and invalidates the cache for associated custom entities.
```php
helper = $helper;
}
/**
* Add custom entity cache tags when product custom entity attributes change
*/
public function afterGetIdentities(Product $source, array $identities): ?array
{
$customEntityProductAttributes = $this->helper->getCustomEntityProductAttributes();
foreach ($customEntityProductAttributes as $customEntityProductAttribute) {
$attributeCode = $customEntityProductAttribute->getAttributeCode();
// Only process if attribute data has changed
if (!$source->hasData($attributeCode) || !$source->dataHasChangedFor($attributeCode)) {
continue;
}
$customEntityIds = $source->getData($attributeCode);
if (is_string($customEntityIds)) {
$customEntityIds = explode(',', $customEntityIds);
}
// Add cache tags for each linked custom entity
foreach ($customEntityIds ?? [] as $customEntityId) {
$identities[] = CustomEntity::CACHE_TAG . '_' . $customEntityId;
}
}
return array_unique($identities);
}
}
```
--------------------------------
### Custom Entity Attribute Source Model (PHP)
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
Provides a source model for custom entity attributes, allowing them to be used in Magento's EAV system. It fetches custom entities filtered by a specific attribute set ID and returns them as options for a dropdown or select input. Relies on a collection factory for custom entities.
```php
customEntityCollectionFactory = $customEntityCollectionFactory;
}
public function getAllOptions(): array
{
$options = [];
// Load custom entities filtered by attribute set
$collection = $this->customEntityCollectionFactory->create()
->addAttributeToSelect('name')
->addAttributeToFilter(
'attribute_set_id',
$this->getAttribute()->getCustomEntityAttributeSetId()
);
foreach ($collection->getItems() as $customEntity) {
$options[] = [
'value' => $customEntity->getId(),
'label' => $customEntity->getName(),
];
}
return $options;
}
}
```
--------------------------------
### JavaScript Admin Configuration for Custom Entity Attributes
Source: https://context7.com/smile-sa/magento2-module-custom-entity-product-link/llms.txt
Configures the visibility and behavior of custom entity attribute fields in the Magento admin panel. This JavaScript code dynamically adjusts the UI based on selected frontend input types, specifically handling the 'smile_custom_entity' type for custom entity attribute set IDs. It ensures related fields are shown or hidden appropriately and manages filterable attribute settings.
```javascript
define([
'jquery',
'Magento_Ui/js/modal/alert',
'Magento_Ui/js/modal/prompt',
'uiRegistry',
'collapsable'
], function ($, alert, prompt, rg) {
'use strict';
return function (optionConfig) {
var swatchProductAttributes = {
frontendInput: $('#frontend_input'),
isFilterable: $('#is_filterable'),
customEntityAttributeSetIdField: $('#custom_entity_attribute_set_id'),
bindAttributeInputType: function () {
this.checkOptionsPanelVisibility();
this.switchDefaultValueField();
// Add 'smile_custom_entity' to filterable frontend inputs
var filterableFrontendInputs = $.merge(
this.selectFields,
['text', 'boolean', 'smile_custom_entity']
);
if (!~$.inArray(this.frontendInput.val(), filterableFrontendInputs)) {
this.isFilterable.selectedIndex = 0;
this._disable(this.isFilterable);
} else {
this._enable(this.isFilterable);
this.backendType.val('int');
}
},
switchDefaultValueField: function () {
var currentValue = this.frontendInput.val();
var defaultCustomEntityAttributeSetId = false;
// Show custom entity attribute set selector
if (currentValue === 'smile_custom_entity') {
defaultCustomEntityAttributeSetId = true;
}
this.setRowVisibility(
this.customEntityAttributeSetIdField,
defaultCustomEntityAttributeSetId
);
}
};
$(function () {
$('#frontend_input').bind('change', function () {
swatchProductAttributes.bindAttributeInputType();
});
swatchProductAttributes.bindAttributeInputType();
});
};
});
```
--------------------------------
### Display Custom Entity Attribute Image in Product Listing (PHTML)
Source: https://github.com/smile-sa/magento2-module-custom-entity-product-link/wiki/AttributeInProductList
This code snippet demonstrates how to display an image from a custom entity attribute within the Magento 2 product listing template. It retrieves custom entity data using a helper class and conditionally displays the first image if available. Ensure the 'Used in Product Listing' option is enabled for the attribute.
```phtml
$pictograms = $this->helper('\Smile\CustomEntityProductLink\Helper\Product')->getCustomEntities($_product, 'picto');
if (!empty($pictograms)) :
?>
```
--------------------------------
### Display Custom Entity Attribute Image in Product Listing (PHTML)
Source: https://github.com/smile-sa/magento2-module-custom-entity-product-link/blob/master/doc/product_attribute_used_in_product_listing.md
This code snippet demonstrates how to retrieve and display an image from a custom entity attribute within a Magento 2 product listing template. It utilizes a helper class to fetch custom entity data and checks for its existence before rendering the image. This is useful for visually representing custom entity information associated with products.
```phtml
$pictograms = $this->helper('\Smile\CustomEntityProductLink\Helper\Product')->getCustomEntities($_product, 'picto');
if (!empty($pictograms)) :
?>
```
--------------------------------
### Add Custom Entity Renderer in Magento 2 (XML)
Source: https://github.com/smile-sa/magento2-module-custom-entity-product-link/wiki/AttributeFrontendRenderer
This configuration allows you to inject custom renderers for specific custom entity types in Magento 2. It targets the `Smile\CustomEntityProductLink\Model\Entity\Attribute\Frontend\CustomEntity` class and adds a new renderer to its arguments. Replace `{entity_type_name}` with your entity's type name and `{EntityType}` with the corresponding class name for your custom renderer.
```xml
- MyCompany\Example\Block\Entity\Attribute\CustomEntity\Renderer\{EntityType}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.