Monthly Archives: February 2018
Imagine you are cooking pasta and sit down to play some game. You listen with one ear to see if the "dish" does not boil away. In this case, you are an observer. When the pasta starts to boil away — the event "pasta_began_to_boil_away" is triggered, which causes you (the observer) to rush to the kitchen (perform an action, an algorithm).
In Magento 2, as in real life, there are observers and events that are implemented on the basis of the "Publish-subscribe pattern". We have already described plugins in Magento 2, which allows you to extend and change the functionality of the store. Let's see how you can do this with the help of events and observers in Magento 2.
Events
Events are triggered by Magento 2 modules when an action has occurred or has to occur. Magento has many native events and also allows you to call your own event.
Use the dispatch method of the \Magento\Framework\Event\ Manager class to call an event.
You often need to change the JavaScript code logic located in the .js file. The easiest way is to override the js file is by using a theme. You can learn how to override view files in the article about Overriding Magento 2 Storeftont Files. This is a quick but not an elegant way.
To change one or more js-file methods, use the mixins available in RequireJS.
To extend this file:
app/code/VendorName/ModuleName/view/%area%/web/js/folder1/folder2/somefile.js
Usually, you get the error bash permission denied when running some script/file that does not have execute permissions. It is one of the most common Magento errors. All you need to do to fix it is to change file permissions and add executive one.
To fix the bash permission denied error follow these steps:
1. Open terminal (shell)
2. Navigate to the folder with the script
3. Run the CLI command to change file permission settings:
chmod +x path_to_file/file_name
For example, if you run a Magento 2 CLI command:
bin/magento ...
and get the error:
bash: bin/magento: Permission denied
You need to add an execute (x) permission to the bin/magento file.