:::: MENU ::::
Posts tagged with: admin panel

Magento 2 change admin session duration

Today we discuss about Magento 2 change admin session duration.Sometime you need to increase the timing of session of your admin. Mostly developer while coding and testing need the admin session to stay there as they switch between browsers pages and editor . Increasing the life of session helps them to avoid logging in to admin again and again.

Login to admin panel and go to

Stores->Settings->Configuration->Advanced->Admin->Security->Admin Session Lifetime (seconds)

And check screenshot following.
Admin session duration

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 31, 2018


Magento2 Add Validation in System Configuration

Today we talk about magento 2 add validation system configuration. In this tutorial we focus on how to add validation in system configuration like required entry, number validation email validation and other validation too. There are already tutorial available about Magento 2 create retrieve system configuration. You can follow that for creating field in system configuration. In this tutorial our focus is how to add validation in system configuration. Now let’s start with our example.

Single Validation class in system configuration

You can add single validation class with required-entry
node.

<field id="title" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Title</label>
                   <validate>required-entry</validate>
   </field>

Multiple Validation class in system configuration

You can add single validation class with required-entry validate-digits
Adding the space between two classes.

<field id="title" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Title</label>
                    <validate>required-entry validate-digits</validate>
   </field>

Available Validation Class

Here is list of available validation classes.

'required-entry'                => 'This is a required field.'
'validate-number'               => 'Please enter a valid number in this field.'
'validate-email'                => 'Please enter a valid email address. For example
[email protected]'

'validate-date'                 => 'Please enter a valid date.'
'validate-select'               => 'Please select an option.'
'validate-no-html-tags'         => 'HTML tags are not allowed'
'validate-digits'               => 'Please use numbers only in this field. Please avoid spaces or other characters such as dots or commas.'
'validate-url'                  => 'Please enter a valid URL. Protocol is required (http://, https:// or ftp://)'
'validate-not-negative-number'  => 'Please enter a number 0 or greater in this field.'
'validate-zero-or-greater'      => 'Please enter a number 0 or greater in this field.'
'validate-state'                => 'Please select State/Province.'
'validate-cc-number'            => 'Please enter a valid credit card number.'
'validate-data'                 => 'Please use only letters (a-z or A-Z), numbers (0-9) or underscore(_) in this field, first character should be a letter.'
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 17, 2018


Magento 2 create retrieve system configuration

Today we talk about how in Magento 2 create retrieve system configuration. This tutorial includes the knowledge of get value of system configuration, how to add your custom configurations in system configurations and also how to retrieve their values too. Configuration values are saved in core_config_data table , in this table you can see the path and value of your configuration settings. So let’s start with our example.

Create system.xml

First step is create system.xml in following directory.

QaisarSatti/HelloWorld/etc/adminhtml/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
     <system>
            <tab id="qaisarsatti_helloworld" translate="label" sortOrder="200">
                <label>QaisarSatti HelloWorld</label>
            </tab>
        <section id="helloworld" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>HelloWorld COnfiguration</label>
            <tab>qaisarsatti_helloworld</tab>
            <resource>qaisarsatti_helloworld::config</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General</label>
                <field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabeld </label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="title" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Title</label>
                </field>                                            
            </group>
        </section>
    </system>
</config>

Retrieve System configuration

For retrieving the values we create the helper file Data.php in following directory

QaisarSatti/HelloWorld/Helper/Data.php

First thing you need to know for retrieving the values in is combination of sestionid/groupid/fieldid to get value.

<?php

namespace QaisarSatti\HelloWorld\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    const HELLOWORLD_MODULE_ENABLED    = 'helloworld/general/enabled';
    const HELLOWORLD_LABEL             = 'helloworld/general/title';



    public function __construct(
        \Magento\Framework\App\Helper\Context $context
    ) {
   
   
   
   
        parent::__construct($context);
    }
    public function getEnabled()
    {
        return $this->scopeConfig->getValue(
            self::HELLOWORLD_MODULE_ENABLED,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
    public function getLabel()
    {
        return$this->scopeConfig->getValue(
            self::HELLOWORLD_LABEL,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

Now: Following you can retrieve system configuration.

Author: Qaisar Satti
Category: Magento 2
Last Modified: August 6, 2018


Magento 2 image upload system configuration

Today we talk about how in Magento 2 image upload system configuration. This tutorial include image upload in system configuration. Sometime you need to add general image for a module, a company logo , a marker or a custom theme. With the help of following code you would be able to add a single image in system configurations.

Add the image field in system configuration.

<field id="image" translate="label comment" type="image" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Store Image</label>
                    <backend_model>QaisarSatti\HelloWorld\Model\Config\Backend\Image</backend_model>
                      <base_url type="media" scope_info="1">image</base_url>
 </field>

Now create model for image upload.

<?php

namespace QaisarSatti\HelloWorld\Model\Config\Backend;

class Image extends \Magento\Config\Model\Config\Backend\Image
{
    /**
     * The tail part of directory path for uploading
     *
     */

    const UPLOAD_DIR = 'store'; // Folder save image

    /**
     * Return path to directory for upload file
     *
     * @return string
     * @throw \Magento\Framework\Exception\LocalizedException
     */

    protected function _getUploadDir()
    {
        return $this->_mediaDirectory->getAbsolutePath($this->_appendScopeInfo(self::UPLOAD_DIR));
    }

    /**
     * Makes a decision about whether to add info about the scope.
     *
     * @return boolean
     */

    protected function _addWhetherScopeInfo()
    {
        return true;
    }

    /**
     * Getter for allowed extensions of uploaded files.
     *
     * @return string[]
     */

    protected function _getAllowedExtensions()
    {
        return ['jpg', 'jpeg', 'gif', 'png', 'svg'];
    }
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 retrieve admin url

Today we talk about how in Magento 2 retrieve admin url. This tutorial included how to get admin url in case you forgot about that. This tutorial show how can you get your admin url from command and also from file. So let start with our example.

File system:

You can also retrieve the the Magento 2 Admin URL in /app/etc/env.php. Example of the relevant file content:

'backend' =>
    array (
       'frontName' => 'admin_test',
    ),

Using Command Line Interface(CLI):

Run the following command to retrieve admin url information.

php bin/magento info:adminuri
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 use plugin

Today we talk about how in Magento 2 use plugin. The plugin or Interceptor is a class the modify the behaviour of public class method by interceptions. The plugin give use three option to use After methods, Before methods and Around Method. This tutorial included example of how to add button on sales order view page. Now Following this example you can use plugin with any public class method in magento 2. So let start with our example.

Create di.xml

First create the di.xml in QaisarSatti/HelloWorld/etc/di.xml if it is not already created. If it is already create just add the plugin code.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="\Magento\Sales\Block\Adminhtml\Order\View">
        <plugin name="QaisarSatti_HelloWorld::AddButtonOrderView" type="QaisarSatti\HelloWorld\Plugin\AddButtonOrderView" />
    </type>
</config>

Create AddButtonOrderView.php

Now we create AddButtonOrderView.php in following directory QaisarSatti\HelloWorld\Plugin. Inject \Magento\Sales\Block\Adminhtml\Order\View in your method to get block obejct.

namespace QaisarSatti\HelloWorld\Plugin;

class AddButtonOrderView
{
   

    public function beforeSetLayout(\Magento\Sales\Block\Adminhtml\Order\View $object)
    {
       
   
            $message = __('Are you sure you want to perform this action?');
  $url = $object->getUrl('addurlhere', ['order_id' =>$object->getOrderId()]);
            $object->addButton(
                'UnCancel',
                [
                    'label' => __('Test Buttonl'),
                    'onclick' => "confirmSetLocation('{$message}', '{$url}')"
                ]
            );
   
    }
}

Note: Following this example you can use plugin with other public class methods too.

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 event observer

Today we talk about how in Magento 2 event observer. This tutorial included observing event sales_order_save_commit_after. Following this example you can observe any event in magento 2. So let start with our example.

Create event.xml

First create the events.xml in QaisarSatti/HelloWorld/etc/events.xml. If you want to observe the event that trigger only the frontend you create events.xml file QaisarSatti/HelloWorld/etc/frontend/events.xml. Same as you want to observe the events that trigger in admin side create file QaisarSatti/HelloWorld/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_save_commit_after">
        <observer name="helloworld_sales_order_save_after" instance="QaisarSatti\HelloWorld\Observer\Observer" />
    </event>
</config>

Create Observer.php

Now we create Observer.php in following directory QaisarSatti\HelloWorld\Observer. Inject \Magento\Framework\Event\Observer in your method to get event data.

<?php
namespace QaisarSatti\HelloWorld\Observer;
use Magento\Framework\Event\ObserverInterface;
 
class Observer implements ObserverInterface {
  public function execute(\Magento\Framework\Event\Observer $observer) {
                 $order = $observer->getEvent()->getOrder(); //get order information      
    }
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Pages:123