Invalid Types of block - magento

I've been failing, trying to add a custom block to my module.
I got the error message :
Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid type of block: Cpy\AcquisitionNumeroTelephonne\Block\Form_lb
Exception #1 (ReflectionException): Class Cpy\AcquisitionNumeroTelephonne\Block\Form_lb does not exist
Nevertheless the class is existing :
Cpy/AcquisitionNumeroTelephonne/Block/Form_lb.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Cpy\AcquisitionNumeroTelephonne\Block;
use Magento\Framework\App\Request\Http;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
class Form_lb extends Template
{
/**
* #var Http
*/
private $request;
/**
* DevisList constructor.
* #param Context $context
* #param Http $request
*/
public function __construct(
Context $context,
Http $request
)
{
$this->request = $request;
parent::__construct($context);
}
}
And here is the layout definition :
Cpy/AcquisitionNumeroTelephonne/view/frontend/layout/acquisition_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>Formulaire acquisition</title>
</head>
<body>
<referenceContainer name="content">
<block class="Cpy\AcquisitionNumeroTelephonne\Block\Form_lb" name="formlb" template="Aims_AcquisitionNumeroTelephonne::form/lb.phtml" cacheable="false" />
</referenceContainer>
</body>
</page>

I finally figure it out. My filename wasn't respecting the PSR convention. So the name Form_lb was invalid.
I had to rename Form_lb to FormLbthen deleting the generated/ and finally bin/magento setup:upgrade

Related

Magento2 Out of Stock Assosiated Products not showing in the dropdown options for Configurable Products

I am facing an issue with Magento2 and I am not sure whether it's a bug or not. I want to show Out of Stock Assosiated Products in the Configurable Product Dropdown.
In the admin settings, Stores -> Configuration -> Catalog -> Inventory -> Display out of stock products, this is already set to "Yes" but no luck.
I have tried several modules with integrating plugins and did a lot of research on this but could not find any solution for this.
Lets take an example - I have a Configurable Product with Child products Size - S , M , L and for instance L is out of stock. So currently I am able to see S and M in the dropdown options for my configurable product.
I want to see all the 3 Child products with the mark of "Out of Stock" like L - Out of Stock or something like that.
sorry for the late reply. I was really busy with different projects.
So for this, i used 1 module and did customized that based on my requirements. I am providing you all the files and folders so anyone who needs this, can upload the files.
Created a module - Jworks/ConfigurableProduct so the folder structure will be /app/code/Jworks/ConfigurableProduct
Create a file app/code/Jworks/ConfigurableProduct/registration.php and enter below code -
<?php
/**
*
* #category Jworks
* #package ConfigurableProduct
* #author Jitheesh V O <jitheesh#digitalboutique.co.uk>
* #copyright Copyright (c) 2018 Digital Boutique (http://www.digitalboutique.co.uk/)
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Jworks_ConfigurableProduct',
__DIR__
);
Create a file app/code/Jworks/ConfigurableProduct/composer.json and enter below code -
{
"name": "jworks/module-configurableproduct",
"description": "Jworks configurableproduct updates",
"require": {
"magento/module-catalog": "101.0.*"
},
"type": "magento2-module",
"version": "1.0.0",
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Jworks\\ConfigurableProduct\\": ""
}
}
}
Create a file app/code/Jworks/ConfigurableProduct/etc/module.xml and enter below code -
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Jworks_ConfigurableProduct" setup_version="0.0.1">
<sequence>
<module name="Magento_ConfigurableProduct" />
</sequence>
</module>
</config>
Create a file app/code/Jworks/ConfigurableProduct/etc/di.xml and enter below code -
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\ConfigurableProduct\Model\ConfigurableAttributeData" type="Jworks\ConfigurableProduct\Model\Rewrite\ConfigurableAttributeData" />
</config>
Create a file app/code/Jworks/ConfigurableProduct/etc/frontend/di.xml and enter below code -
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface">
<plugin name="Magento_ConfigurableProduct_Plugin_Model_ResourceModel_Attribute_InStockOptionSelectBuilder" type="Jworks\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute\InStockOptionSelectBuilder"/>
</type>
<type name="Magento\ConfigurableProduct\Model\AttributeOptionProvider">
<plugin name="Magento_ConfigurableProduct_Plugin_Model_AttributeOptionProvider" type="Jworks\ConfigurableProduct\Plugin\Model\AttributeOptionProvider"/>
</type>
<type name="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable">
<plugin name="changeAllowProductsBehaviour" type="Jworks\ConfigurableProduct\Model\ConfigurableProduct\Block\Product\View\Type\Configurable\Plugin" sortOrder="10" />
</type>
</config>
Create a file app/code/Jworks/ConfigurableProduct/Plugin/Model/AttributeOptionProvider.php and enter below code -
<?php
namespace Jworks\ConfigurableProduct\Plugin\Model;
class AttributeOptionProvider
{
public function afterGetAttributeOptions(\Magento\ConfigurableProduct\Model\AttributeOptionProvider $subject, array $result)
{
$optiondata=array();
foreach ($result as $option) {
if(isset($option['stock_status']) && $option['stock_status']==0){
$option['option_title'] = $option['option_title'].__(' - uitverkocht');
}
$optiondata[]=$option;
}
return $optiondata;
}
}
Create a file app/code/Jworks/ConfigurableProduct/Plugin/Model/ResourceModel/Attribute/InStockOptionSelectBuilder.php and enter below code -
<?php
namespace Jworks\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute;
use Magento\CatalogInventory\Model\ResourceModel\Stock\Status;
use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface;
use Magento\Framework\DB\Select;
/**
* Plugin for OptionSelectBuilderInterface to add stock status filter.
*/
class InStockOptionSelectBuilder
{
/**
* CatalogInventory Stock Status Resource Model.
*
* #var Status
*/
private $stockStatusResource;
/**
* #param Status $stockStatusResource
*/
public function __construct(Status $stockStatusResource)
{
$this->stockStatusResource = $stockStatusResource;
}
/**
* Add stock status filter to select.
*
* #param OptionSelectBuilderInterface $subject
* #param Select $select
* #return Select
*
* #SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetSelect(OptionSelectBuilderInterface $subject, Select $select)
{
$select->joinInner(
['stock' => $this->stockStatusResource->getMainTable()],
'stock.product_id = entity.entity_id',
['stock.stock_status']
);
return $select;
}
}
Create a file app/code/Jworks/ConfigurableProduct/Model/Rewrite/ConfigurableAttributeData.php and enter below code -
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Model;
use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;
/**
* Class ConfigurableAttributeData
* #api
* #since 100.0.2
*/
class ConfigurableAttributeData
{
/**
* Get product attributes
*
* #param Product $product
* #param array $options
* #return array
*/
public function getAttributesData(Product $product, array $options = [])
{
$defaultValues = [];
$attributes = [];
foreach ($product->getTypeInstance()->getConfigurableAttributes($product) as $attribute) {
$attributeOptionsData = $this->getAttributeOptionsData($attribute, $options);
if ($attributeOptionsData) {
$productAttribute = $attribute->getProductAttribute();
$attributeId = $productAttribute->getId();
$attributes[$attributeId] = [
'id' => $attributeId,
'code' => $productAttribute->getAttributeCode(),
'label' => $productAttribute->getStoreLabel($product->getStoreId()),
'options' => $attributeOptionsData,
'position' => $attribute->getPosition(),
];
$defaultValues[$attributeId] = $this->getAttributeConfigValue($attributeId, $product);
}
}
return [
'attributes' => $attributes,
'defaultValues' => $defaultValues,
];
}
/**
* #param Attribute $attribute
* #param array $config
* #return array
*/
protected function getAttributeOptionsData($attribute, $config)
{
$attributeOptionsData = [];
foreach ($attribute->getOptions() as $attributeOption) {
$optionId = $attributeOption['value_index'];
$attributeOptionsData[] = [
'id' => $optionId,
'label' => $attributeOption['label'],
//'test' => $config[$attribute->getAttributeId()][$optionId],
'products' => isset($config[$attribute->getAttributeId()][$optionId])
? $config[$attribute->getAttributeId()][$optionId]
: [],
];
}
return $attributeOptionsData;
}
/**
* #param int $attributeId
* #param Product $product
* #return mixed|null
*/
protected function getAttributeConfigValue($attributeId, $product)
{
return $product->hasPreconfiguredValues()
? $product->getPreconfiguredValues()->getData('super_attribute/' . $attributeId)
: null;
}
}
Create a file app/code/Jworks/ConfigurableProduct/Model/ConfigurableProduct/Block/Product/View/Type/Configurable/Plugin.php and enter below code -
<?php
namespace Jworks\ConfigurableProduct\Model\ConfigurableProduct\Block\Product\View\Type\Configurable;
class Plugin
{
/**
* getAllowProducts
*
* #param \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject
*
* #return array
*/
public function beforeGetAllowProducts($subject)
{
if (!$subject->hasData('allow_products')) {
$products = [];
$allProducts = $subject->getProduct()->getTypeInstance()->getUsedProducts($subject->getProduct(), null);
foreach ($allProducts as $product) {
$products[] = $product;
}
$subject->setData('allow_products', $products);
}
return [];
}
}
And after all these, you can run all the commands for installing the Magento2 extension.
I hope anybody can get help with this.
open the file vendor\magento\module-catalog\Helper\Product.php
change
protected $_skipSaleableCheck = false;
to
protected $_skipSaleableCheck = true;

Method open does not exist, Laravel, Form facade

I am learning laravel following a tutorial. I am stuck with this error
ErrorException in Macroable.php line 81:
Method open does not exist. (View: path\to\project\resources\views\form.blade.php)
I am using FormFacade. earlier I was facing an error saying:
Call to undefined method Illuminate\Foundation\Application::bindShared()
which I overcame by replacing bindShared with singleton all over the file
/path/project/vendor/illuminate/html/HtmlServiceProvider.php
form.blade.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<h1>Create a new form</h1>
<hr/>
{{ Form::open() }}
{{ Form::close() }}
</body>
</html>
HtmlServiceProvider.php
use Illuminate\Support\ServiceProvider;
class HtmlServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->registerHtmlBuilder();
$this->registerFormBuilder();
$this->app->alias('html', 'Illuminate\Html\HtmlBuilder');
$this->app->alias('form', 'Illuminate\Html\FormBuilder');
}
/**
* Register the HTML builder instance.
*
* #return void
*/
protected function registerHtmlBuilder()
{
$this->app->singleton('html', function($app)
{
return new HtmlBuilder($app['url']);
});
}
/**
* Register the form builder instance.
*
* #return void
*/
protected function registerFormBuilder()
{
$this->app->singleton('form', function($app)
{
$form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());
return $form->setSessionStore($app['session.store']);
});
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return array('html', 'form');
}
}
Plese Help.
illuminate/html was deprecated for Laravel 5.0, and has not been updated to work with Laravel 5.1+.
You need to replace it with the laravelcollective/html package.
Add in your config/app.php provider:
Collective\Html\HtmlServiceProvider::class,
and add alias:
'Html' => Collective\Html\HtmlFacade::class,
and replace form open and close :
{!! Form::open() !!}
{!! Form::close() !!}
notes:
This is for laravel 5.

How to save data using Model in Magento2

I have a basic module ready with controller and view working perfectly. Now, I am trying to initiate model in order to save data using custom model in table containing attributes (question Title, Question). Basically, what steps I should proceed in order to save data through model in a custom table ?
How should I do it, any help would be greatly appreacited.
I have below code in my action file :
class Post extends \Magento\Framework\App\Action\Action
{
protected $_objectManager;
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
{
$this->_objectManager = $objectManager;
}
public function execute()
{
$post = $this->getRequest()->getPostValue();
$model = $this->_objectManager->create('Chirag\Mygrid\Model\Question');
$model->setData('question_title', $post['question_title']);
$model->setData('question', $post['question']);
$model->save();
}
}
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\Mygrid\Model;
class Question extends \Magento\Framework\Model\AbstractModel
{
/**
* Initialize resource model
*
* #return void
*/
protected function _construct()
{
$this->_init('Chirag\Mygrid\Model\Resource\Question');
}
}
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\Mygrid\Model\Resource;
class Question extends \Magento\Framework\Model\Resource\Db\AbstractDb
{
/**
* Initialize resource model
*
* #return void
*/
protected function _construct()
{
$this->_init('questions_and_answers', 'question_id');
}
}
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\Mygrid\Model\Resource\Question;
class Collection extends \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection
{
protected function _construct()
{
$this->_init('Chirag\Mygrid\Model\Question', 'Chirag\Mygrid\Model\Resource\Question');
$this->_map['fields']['page_id'] = 'main_table.page_id';
}
/**
* Prepare page's statuses.
* Available event cms_page_get_available_statuses to customize statuses.
*
* #return array
*/
public function getAvailableStatuses()
{
return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
}
}
1st Edit:
I had successfully completed this and have uploaded the whole setup to Git, please find below URL in case you want to check it out :
FAQ with custom grid extension in Magento 2
You can save Data like this way in Magento 2:
$question = $this->_objectManager->create('Ecom\HelloWorld\Model\Question');
$question->setTitle('Simple Question');
$question->setDescription('Question Description');
$question->save();
this is the code that you will add in your particular action
Updated Answer
Add your question model as follows:
namespace Chirag\Mygrid\Model;
class Question extends \Magento\Framework\Model\AbstractModel
{
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
public function _construct()
{
$this->_init('Chirag\Mygrid\Model\Resource\Question');
}
}
Also please flush cache as well.
Please follow here as well: In magento 2 what is the correct way for getModel?

Custom module not working in Magento2

I have been trying to setup a basic module in Magento2, it keeps throwing 404 despite doing all the ideal changes. Below is the code related to the module. My vendor name is Chirag and module name is HelloWorld.
/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Chirag_HelloWorld" schema_version="0.0.1" setup_version="0.0.1">
</module>
</config>
/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/frontend/route.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="helloworld" frontName="helloworld">
<module name="Chirag_HelloWorld" />
</route>
</router>
</config>
/var/www/html/magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
/**
*
*
* #return void
*/
public function execute()
{
protected $resultPageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
return $this->resultPageFactory->create();
}
}
}
/var/www/html/magento2/app/code/Chirag/HelloWorld/Block/HelloWorld.php
<?php
namespace Chirag\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
}
/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_index_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Chirag\HelloWorld\Block\HelloWorld" name="helloworld" template="helloworld.phtml" />
</referenceContainer>
</body>
</page>
/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
<h1> test hello to Magento 2 !! </h1>
Any kind of help would be really appreciated.
First try to rename route.xml to routes.xml and see if that fixes the issue.
Next try changing your code in the controller, try change company/module names:
<?php
namespace YourCompany\ModuleName\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
return $this->resultPageFactory->create();
}
}
also in your helloworld_index_index.xml you can try changing the template decoration to:
template="YourCompany_ModuleName::templatename.phtml"
lastly you can try changing the module.xml setup_version declaration to:
setup_version="2.0.0"
I hope this helps!
Below changes led to correct answer for me :
Index controller - magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* Show Contact Us page
*
* #return void
*/
public function execute()
{
$this->_view->loadLayout();
$this->_view->getLayout()->getBlock('helloworld');
$this->_view->renderLayout();
}
}
Block - magento2/app/code/Chirag/HelloWorld/Block/Question.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Block;
use Magento\Framework\View\Element\Template;
/**
* Main contact form block
*/
class Question extends Template
{
/**
* #param Template\Context $context
* #param array $data
*/
public function __construct(Template\Context $context, array $data = [])
{
parent::__construct($context, $data);
$this->_isScopePrivate = true;
}
}
Layout file - magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_index_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Chirag\HelloWorld\Block\Question" name="helloworld" template="Chirag_HelloWorld::helloworld.phtml">
</block>
</referenceContainer>
</body>
</page>
View template - magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
<?php echo 'helloworld view'; ?>
<h1> Hello World </h1>
You need to follow the below steps to resolve:
You have to change route.xml to routes.xml
Run setup upgrade command:
php bin/magento s:up
Go to your_base_link/helloworld/index/index
Enjoin your result
First of all, rename route.xml to routes.xml
app/code/Chirag/HelloWorld/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="helloworld" frontName="helloworld">
<module name="Chirag_HelloWorld" />
</route>
</router>
</config>
Index Controller - app/code/Chirag/HelloWorld/Controller/Index/Index.php
You can't use __construct inside the execute method and these two should be separated.
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Controller\Index;
use Magento\Framework\App\Action\Action;
class Index extends Action
{
/**
* #var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* #param \Magento\Framework\App\Action\Context $context
* #param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
/**
* #return mixed
*/
public function execute()
{
return $this->resultPageFactory->create();
}
}
You should use the below format when you are calling the template in layout xml.
eg: Chirag_HelloWorld::helloworld.phtml
app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_index_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Chirag\HelloWorld\Block\HelloWorld" name="helloworld" template="Chirag_HelloWorld::helloworld.phtml" />
</referenceContainer>
</body>
</page>
app/code/Chirag/HelloWorld/Block/HelloWorld.php
<?php
namespace Chirag\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
public function getHelloWorldText()
{
return __('Hello World');
}
}
app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
<?= /* #noEscape */ $block->getHelloWorldText() ?>
Once you done run the below commands to apply the changes.
bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento c:f
Use -f as a parameter if you are in developer mode.
Use bin/magento deploy:mode:show to check the mode.
Happy Coding!!

Magento Admin Custom Tab in Catalog Product

I have created a custom tab under catalog product, the tab is coming fine but when i click on the tab the template for the block is not loading. It echo what i type but it is not fetching the template. I write this function in a class HTC_Csvpricing_Block_Adminhtml_Catalog_Product_Tab extends Mage_Adminhtml_Block_Template implements Mage_Adminhtml_Block_Widget_Tab_Interface
public function _construct()
{
parent::_construct();
echo "I m here";
$this->setTemplate('csvpricing/tab.phtml');
}
Here what I write in app/design/adminhtml/default/default/layout/csvpricing.xml
<csvpricing_adminhtml_csvpricing_index>
<reference name="content">
<block type="csvpricing/adminhtml_csvpricing" name="csvpricing" />
</reference>
</csvpricing_adminhtml_csvpricing_index>
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<action method="addTab">
<name>csvpricing_tab</name>
<block>csvpricing/adminhtml_catalog_product_tab</block>
</action>
</reference>
</adminhtml_catalog_product_edit>
Please guide what I am missing.
Thanks
You can do as follow :
Module config.xml
<config>
<adminhtml>
<layout>
<updates>
<your_module>
<file>your-module.xml</file>
</your_module>
</updates>
</layout>
</adminhtml>
</config>
Create the layout XML in /app/design/adminhtml/default/default/layout/your-module.xml:
<?xml version="1.0"?>
<layout>
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<action method="addTab">
<name>your_module_some_tab</name>
<block>your_module/adminhtml_catalog_product_tab</block>
</action>
</reference>
</adminhtml_catalog_product_edit>
</layout>
Create the tab block in {your_module}/Block/Adminhtml/Catalog/Product/Tab.php:
class Your_Module_Block_Adminhtml_Catalog_Product_Tab
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
/**
* Set the template for the block
*/
public function _construct()
{
parent::_construct();
$this->setTemplate('catalog/product/tab/some-tab.phtml');
}
/**
* Retrieve the label used for the tab relating to this block
*
* #return string
*/
public function getTabLabel()
{
return $this->__('Some Tab');
}
/**
* Retrieve the title used by this tab
*
* #return string
*/
public function getTabTitle()
{
return $this->__('Some Tab');
}
/**
* Determines whether to display the tab
* Add logic here to decide whether you want the tab to display
*
* #return bool
*/
public function canShowTab()
{
return true;
}
/**
* Stops the tab being hidden
*
* #return bool
*/
public function isHidden()
{
return false;
}
}
you need to create a data helper to support translation, if your module does not have one already.
Make the tabs template in /app/design/adminhtml/default/default/template/catalog/product/tab/some-tab.phtml:
<div class="entry-edit">
<div class="entry-edit-head">
<h4>Some Heading</h4>
</div>
<div class="fieldset fieldset-wide">
<div class="hor-scroll">
<!-- your content here -->
</div>
</div>
</div>

Resources