Posts

How to sanitize request in a magento 2 controller?

  How to sanitize request in a magento 2 controller? To sanitize request data in a Magento 2 controller, you can use the built-in filtering system. Here's a simple example of how to do it: First, let's make sure we are in the correct file. In your Magento 2 module, go to the Controller folder and find the PHP file that handles the request you want to sanitize. Next, add the  \Magento\Framework\Data\Form\FormKey\Validator  class and the  \Magento\Framework\App\Request\DataPersistorInterface  class to the constructor of your controller class. This will allow you to validate the form key and persist data across requests. Here's an example of what your constructor might look like: public function __construct ( \ Magento \ Framework \ App \ Action \ Context $context , \ Magento \ Framework \ Data \ Form \ FormKey \ Validator $formKeyValidator , \ Magento \ Framework \ App \ Request \ DataPersistorInterface $dataPersistor ) { $this -> formKeyValidato

The Object Manager and the Dependency Injection

  Can you say what advantages can have the Object Manager over Dependency Injection? Actually, Dependency Injection (DI) is the recommended approach in Magento 2, while using the Object Manager (OM) directly is discouraged. However, there are a few cases where using the Object Manager could have some advantages: Flexibility: The Object Manager allows you to create objects dynamically at runtime. This can be useful in specific situations where you need to instantiate objects based on certain conditions or input. Ease of use for quick testing: The Object Manager can be used for quick testing or prototyping purposes, where you want to test a piece of code without going through the process of injecting dependencies. However, it's important to note that using the Object Manager directly has several disadvantages and is considered an anti-pattern in Magento 2: Harder to maintain and test: Since the Object Manager is responsible for instantiating objects, it can make the code harder to ma

References and blocks

  How to set in magento xml layout a new block class for an existing block? Also how to set it not cacheable? To set a new block class for an existing block in Magento 2, you can use the "referenceBlock" tag in your XML layout file. Here's a simple example: Find the block's name. It is usually set in the XML layout file of the module where the block is created. Create your own custom block class by extending the original block class in your module. In your module's XML layout file, use the "referenceBlock" tag to update the existing block's class. Here's an example of the XML layout code: < page xmlns: xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi: noNamespaceSchemaLocation = " urn:magento:framework:View/Layout/etc/page_configuration.xsd " > < body > < referenceBlock name = " block_name " > < arguments > < argument name = " bloc

AfterGetProductCollection plugin hassle with SearchResultApplier

    I wrote an AfterGetProductCollection plugin for Magento\CatalogSearch\Model\Advanced updating the resulting collection. I am using Elasticsearch and Magento Commerce. The problem is that Magento\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplier is not aware of the collection updates and applies the original search results product ids. Ah, I see! You're working on a plugin to modify the product collection in Magento with Elasticsearch. To address the issue with the SearchResultApplier not being aware of the collection updates, you may need to extend the SearchResultApplier itself. You can create a custom module and use dependency injection (DI) to override the original SearchResultApplier class. In your custom module's  di.xml  file, you can add a preference to point to your new class that extends the original SearchResultApplier. This way, Magento will use your custom class instead of the original one. Here's an example for your  di.xml  file: &l