How to use Magento 2 UI components in a custom module? - magento2

I'm trying to use Magento 2.4 UI components on a custom module. As per the documentation, I should add a dependency for the Magento_UI module:
To use UI components in your custom module, you need to add a dependency for the Magento_UI module in your component’s composer.json file.
https://devdocs.magento.com/guides/v2.4/ui_comp_guide/bk-ui_comps.html
How can I do that? I've tried to add the following line in my composer.json but this is obviously not the right syntax:
"require": {
"Magento_UI": "*"
},

To require a Magento module in your composer.json, you just need to add the required line in the "require" section of your composer.json:
{
...
"require": {
"magento/module-ui": "*"
},
...
}
You can find the list of Magento modules here: https://devdocs.magento.com/guides/v2.4/architecture/archi_perspectives/components/modules/mod_depend.html#hard-dependencies
...
"require": {
"magento/module-catalog": "103.0.*",
"magento/module-email": "101.0.*",
"magento/module-media-storage": "100.3.*",
"magento/module-store": "101.0.*",
"magento/module-theme": "101.0.*",
"magento/module-ui": "101.1.*",
"magento/module-variable": "100.3.*",
"magento/module-widget": "101.1.*",
"magento/module-authorization": "100.3.*"
}
...

Related

Yii2 fork: dev-custom exists as yiisoft/yii2 but these are rejected by your constraint

I'm trying to use my forked version of yiisoft/yii2 in an installation of yii2-app-advanced.
I've followed this wiki.
I have created a branch named custom and pushed it to my yii2 fork.
In the composer.json of my fork I have the following (does it matter?):
...
"extra": {
"branch-alias": {
"dev-custom": "2.0.x-dev"
}
...
Then in my yii2-app-advanced installation I have the following composer.json:
{
"name": "yiisoft/yii2-app-advanced",
...
"minimum-stability": "stable",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/david-it/yii2"
}
],
"require": {
"php": ">=5.6.0",
"yiisoft/yii2": "dev-custom",
"yiisoft/yii2-swiftmailer": "~2.0.0 || ~2.1.0",
"yiisoft/yii2-bootstrap4": "~2.0.6"
},
...
}
When I run composer update I get the following error:
Problem 1
- The requested package yiisoft/yii2 dev-custom exists as yiisoft/yii2[2.0.0, 2.0.0-alpha, 2.0.0-beta, 2.0.0-rc, 2.0.1, 2.0.10, 2.0.11, 2.0.11.1, 2.0.11.2, 2.0.12, 2.0.12.1, 2.0.12.2, 2.0.13, 2.0.13.1, 2.0.13.2, 2.0.13.3, 2.0.14, 2.0.14.1, 2.0.14.2, 2.0.15, 2.0.15.1, 2.0.16, 2.0.16.1, 2.0.17, 2.0.18, 2.0.19, 2.0.2, 2.0.20, 2.0.21, 2.0.22, 2.0.23, 2.0.24, 2.0.25, 2.0.26, 2.0.27, 2.0.28, 2.0.29, 2.0.3, 2.0.30, 2.0.31, 2.0.32, 2.0.4, 2.0.5, 2.0.6, 2.0.7, 2.0.8, 2.0.9, 2.1.x-dev, 3.0.x-dev, dev-master, 2.0.x-dev] but these are rejected by your constraint.
The command composer why-not yiisoft/yii2 dev-custom (if it is any useful here) shows this:
yiisoft/yii2-bootstrap4 2.0.8 requires yiisoft/yii2 (~2.0)
yiisoft/yii2-debug 2.1.13 requires yiisoft/yii2 (~2.0.13)
yiisoft/yii2-faker 2.0.4 requires yiisoft/yii2 (~2.0.0)
yiisoft/yii2-gii 2.1.4 requires yiisoft/yii2 (~2.0.14)
yiisoft/yii2-swiftmailer 2.1.2 requires yiisoft/yii2 (>=2.0.4)
I'm not sure what I am missing. I've looked around including this and this questions but with no luck.
EDIT 1
Composer 1.9.3 (2020-02-04 12:58:49) with PHP 7.2.24-0ubuntu0.18.04.3 on Linux / 4.15.0-91-generic
EDIT 2
Selecting dev-master as required works but the original files are downloaded (not the ones in the fork).
"require": {
...
"yiisoft/yii2": "dev-master",
...
}
I also tried all the tips from this answer with no luck.
At the moment, you have the custom branch. You need to create a branch named dev-custom in this situation, use the same branch name in require section of your composer.json file.
Your custom branch name must be prefixed with dev-.
After long hours of trial-and-error I've found two working solutions.
Solution 1 (With the help of Yii2 forum)
The branch apparently is considered "unstable", so you need to specify #dev to make composer work fine:
"require": {
...
"yiisoft/yii2": "dev-master#dev",
...
}
Solution 2
In the composer.json of my fork I've added an alias pointing to the branch:
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev",
"dev-custom": "2.0.n-dev"
}
}
Where n is just number (it should be unique among the versions available, I guess).
Then in the composer.json of my project I used "yiisoft/yii2": "2.0.n.x-dev".
I'm not quite sure why I have to use "2.0.n.x-dev" instead of "2.0.n-dev". A possible explanation is here.

TYPO3 tutorial extension, controller does not exist

I'm trying to get started with TYPO3 extensions and was following this tutorial to get to see the basics.
In the backend everything works fine, but on the front end I get an error:
Oops, an error occurred! Code: 20170209104827c3b58d58 -
{"exception":"exception 'ReflectionException' with message 'Class
Tx_Inventory_Controller_InventoryController does not exist'
My files are exactly the same as in the tutorial. I have no idea what is causing this. I assume I made some dumb mistake with namespaces, but they seem to be all correct.
The controller class can be found below and is located in typo3conf/ext/inventory/Classes/Controller/
<?php
namespace \MyVendor\Inventory\Controller;
use \TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use \TYPO3\CMS\Core\Utility\GeneralUtility;
use \MyVendor\Inventory\Domain\Model\Repository\ProductRepository;
class InventoryController extends ActionController {
public function listAction() {
$productRepository = GeneralUtility::makeInstance(ProductRepository::class)
$products = $productRepository->findAll();
$this->view->assign('products', $products);
}
}
?>
When developing a new extension in a composer installed TYPO3 V9 (here: 9.4) the autoload part has to be added to the central root composer.json. Found it here (German). Following the steps in the OPs mentioned tutorial leads to a core exception:
Core: Exception handler (WEB): Uncaught TYPO3 Exception: #1278450972:
Class MyVendor\StoreInventory\Controller\StoreInventoryController does not exist.
Reflection failed.
As long as the extension is not installed via composer, e.g because it's newly developed, composer does not find the appropriate composer.json file in the extensions directory. Hence TYPO3 does not find any classes in the new extensions Classes directory. To resolve the issue the autoload configuration has to be added to the root composer.json. Just put the following lines into composer.json within the installations base directory:
{
"repositories": [
{ "type": "composer", "url": "https://composer.typo3.org/" }
],
...
"autoload": {
"psr-4": {
"MyVendor\\StoreInventory\\": "public/typo3conf/ext/store_inventory/Classes/"
}
}
}
Then regenerate the autoload configuration:
composer dumpautoload
You possibly have to clear the cache as well in the backend.
It looks like your class is not autoloaded. If you don't use composer to make your autoload, take a look in your typo3conf/autoload/autoload_classmap.php file.
You should find an entry corresponding to your file. You will see if you have a path error.
Remove backslashes - try with
<?php
namespace MyVendor\Inventory\Controller;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use MyVendor\Inventory\Domain\Model\Repository\ProductRepository;
class InventoryController extends ActionController {
public function listAction() {
$productRepository = GeneralUtility::makeInstance(ProductRepository::class)
$products = $productRepository->findAll();
$this->view->assign('products', $products);
}
}
Ensure you add Vendorname to extension key, when you register your plugin, see ext_tables.php and write 'MyVendor.'.$_EXTKEY instead of $_EXTKEY like
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin(
'MyVendor.'.$_EXTKEY,
'List',
'The Inventory List'
);
I had exactly the same problem - it happens if Typo3 installation is done by composer. To solve this problem see this page of the docs.
Try to add autoload in your ext_emconf.php (replace 'Vendor\\Extensionkey\\') and uninstall and install your extension again (to rebuild PHP autoload information)
'autoload' =>
array (
'psr-4' =>
array (
'Vendor\\Extensionkey\\' => 'Classes',
),
),
'_md5_values_when_last_written' => 'a:0:{}',
'suggests' => array(
),

How can I use my webpack's html-loader imports in Jest tests?

I am just getting started with the Jest test framework and while straight up unit tests work fine, I am having massive issues testing any component that in its module (ES module via babel+webpack) requires a HTML file.
Here is an example:
import './errorHandler.scss';
import template from './errorHandler.tmpl';
class ErrorHandler {
...
I am loading the component specific SCSS file which I have set in Jest's package.json config to return an empty object but when Jest tries to run the import template from './errorHandler.tmpl'; line it breaks saying:
/Users/jannis/Sites/my-app/src/scripts/errorHandler/errorHandler.tmpl.html:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){<div class="overlay--top">
^
SyntaxError: Unexpected token <
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:284:10)
My Jest config from package.json is as follows:
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>/test/setupFile.js",
"moduleDirectories": ["node_modules"],
"moduleFileExtensions": ["js", "json", "html", "scss"],
"moduleNameMapper": {
"^.+\\.scss$": "<rootDir>/test/styleMock.js"
}
}
It seems that the webpack html-loader is not working correctly with Jest but I can't find any solution on how to fix this.
Does anyone know how I can make these html-loader imports work in my tests? They load my lodash template markup and i'd rather not have these at times massive HTML chunks in my .js file so i can omit the import template from x part.
PS: This is not a react project, just plain webpack, babel, es6.
I encountered this specific problem recently and creating your own transform preprocesser will solve it. This was my set up:
package.json
"jest": {
"moduleFileExtensions": [
"js",
"html"
],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.html$": "<rootDir>/test/utils/htmlLoader.js"
}
}
NOTE: babel-jest is normally included by default, but if you specify a custom transform preprocessor, you seem to have to include it manually.
test/utils/htmlLoader.js:
const htmlLoader = require('html-loader');
module.exports = {
process(src, filename, config, options) {
return htmlLoader(src);
}
}
A bit late to the party, but wanted to add that there is also this html-loader-jest npm package out there to do this if you wanted to go that route.
Once you npm install it you will add it to your jest configuration with
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.html?$": "html-loader-jest"
}
For Jest > 28.x.x with html-loader:
Create a custom transformer as documented here.
jest/html-loader.js
const htmlLoader = require("html-loader");
module.exports = {
process(sourceText) {
return {
code: `module.exports = ${htmlLoader(sourceText)};`,
};
},
};
Add it to your jest config.
jest.config.js
...
// A map from regular expressions to paths to transformers
transform: {
"^.+\\.html$": "<rootDir>/jest/html-loader.js",
},
...
It will fix the error : Invalid return value: process() or/and processAsync() method of code transformer found at "<PATH>" should return an object or a Promise resolving to an object.
Maybe your own preprocessor file will be the solution:
ScriptPreprocessor
Custom-preprocessors
scriptpreprocessor: The path to a module that provides a synchronous function from pre-processing source files. For example, if you wanted to be able to use a new language feature in your modules or tests that isn't yet supported by node (like, for example, ES6 classes), you might plug in one of many transpilers that compile ES6 to ES5 here.
I created my own preprocessor when I had a problems with my tests after added transform-decorators-legacy to my webpack module loaders.
html-loader-jest doesn't work for me. My workaround for this:
"transform": {
'\\.(html)$': '<rootDir>/htmlTemplateMock.html'
}
htmlTemplateMock.html is empty file
For Jest 28+ you can use jest-html-loader to make Jest work with code that requires HTML files.
npm install --save-dev jest-html-loader
In your jest config, add it as a transformer for .HTML files:
"transform": {
"^.+\\.html?$": "jest-html-loader"
},

Include Zend In Symfony 2

I have a problem for including Zend Framework in Symfony 2 IN PRODUCTION, because when i use it on local there is no problem ...
I just commited my work on my production server and i have this error :
Fatal error: Class 'Zend_Gdata_AuthSub' not found
And there is this error for any classes of Zend Framework ...
This is my autoload which is good for localhost :
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
$loader->add('Zend_', __DIR__.'/../vendor/zf/library');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
set_include_path(__DIR__.'/../vendor/zf/library'.PATH_SEPARATOR.get_include_path());
return $loader;
?>
There is probably a problem with the include path but i don't know why ...
Thanks a lot !
If you want to use Composer to pull in the components you need from ZF2 then you could use the information at Zend Framework site Composer info page
As an example you can add code like this to your composer.json file to enable the repository:
"repositories": [
{
"type": "composer",
"url": "https://packages.zendframework.com/"
}
],
"require": {
"zendframework/zend-config": "2.0.*",
"zendframework/zend-http": "2.0.*"
},
You place the names of the packages you want to pull in under the "require" section and the list of available packages is at the link I supplied so you can check the names there.
When you go to install the dependencies you can use this command:
php composer.phar install
Does that help? :-)

The module "taggableComplete" is not enabled

I'm trying to add the tag widget from the plugin sfDoctrineActAsTaggablePlugin
I've added "_tag" to my configuration.yml, and in _tag.php I wrote :
<?php include_component('taggableComplete','tagWidget', array('object' => $form->getObject())) ?>
But all I get now is the following error :
"The module "taggableComplete" is not
enabled"
What am I missing ?
You have to enable modules you want to use after installing a plugin.
tagWidget component is defined in taggableComplete module. As it's a module from the plugin you have explicitly enable it.
You can do it in your application's settings.yml file:
all:
.settings:
enabled_modules: [default, taggableComplete]