9 – How do I connect to a database while building a custom module?

9 – How do I connect to a database while building a custom module? – Drupal Answers

How to connect to a mysql/mariadb database in Drupal 9 while building a custom module

Are you pulling data with AJAX or plain , entity_field ?

Again, is the pulled data being implemented in entity, node or block .

Now comes – database API, add some code which API you seem, not working in Drupal-9.

The database API, working version – As documented by Drupal Official Documentation –
Drupal Database API and Drupal 8 dynamic query

Unless you required to do do not use full query as Verbatim, instead use Drupal API,

Drupal API based Select query

Drupal::service('database')->select('users', 'u');
  ->condition('u.uid', 0, '<>')
  ->fields('u', ('uid', 'name', 'status', 'created', 'access'))
  ->range(0, 50);

The Drupal::service('database') can be Drupal::database.

When, this API is not working, you can write in Native PHP, and get the Array or Object, you have stored.

Your question is incomplete to understand. Yes, sometimes the insert or select and merge even update API is hard to get worked. insert or update will not fail you.

But merge is hard to get work, sometimes – select can fail.

module – Magento 2.4.2: How to change the shipping label?

With a custom shipping module, I am adding brokerage and duties to the shipping cost for an order from Canada. How can the shipping label be changed so that it reads “Shipping includes brokerage and duties”

enter image description here

Below is entire customshipping.php script


namespace PerfectmakeupmirrorsCustomShippingModelCarrier;

use MagentoQuoteModelQuoteAddressRateRequest;
use MagentoShippingModelCarrierAbstractCarrier;
use MagentoShippingModelCarrierCarrierInterface;
use PerfectmakeupmirrorsPmmFedexModelPmmFedexCarrier;
use MagentoFrameworkAppResourceConnection;

 * Custom shipping model
class Customshipping extends AbstractCarrier implements CarrierInterface
    const FEDEX_METHODS = (
        'EUROPE_FIRST_INTERNATIONAL_PRIORITY' => 'Europe First Priority',
        'FEDEX_1_DAY_FREIGHT' => '1 Day Freight',
        'FEDEX_2_DAY_FREIGHT' => '2 Day Freight',
        'FEDEX_2_DAY' => '2 Day',
        'FEDEX_2_DAY_AM' => '2 Day AM',
        'FEDEX_3_DAY_FREIGHT' => '3 Day Freight',
        'FEDEX_EXPRESS_SAVER' => 'Express Saver',
        'FEDEX_GROUND' => 'Ground',
        'FIRST_OVERNIGHT' => 'First Overnight',
        'GROUND_HOME_DELIVERY' => 'Home Delivery',
        'INTERNATIONAL_ECONOMY' => 'International Economy',
        'INTERNATIONAL_ECONOMY_FREIGHT' => 'Intl Economy Freight',
        'INTERNATIONAL_FIRST' => 'International First',
        'INTERNATIONAL_GROUND' => 'International Ground',
        'INTERNATIONAL_PRIORITY' => 'International Priority',
        'INTERNATIONAL_PRIORITY_FREIGHT' => 'Intl Priority Freight',
        'PRIORITY_OVERNIGHT' => 'Priority Overnight',
        'SMART_POST' => 'Smart Post',
        'STANDARD_OVERNIGHT' => 'Standard Overnight',
        'FEDEX_FREIGHT' => 'Freight',
        'FEDEX_NATIONAL_FREIGHT' => 'National Freight'
    const SHIPPING_2ND_DAY = '2DY';
    protected $_shipping_mode_strings = array(
        self::SHIPPING_STANDARD => 'Standard Ground',
        self::SHIPPING_2ND_DAY => 'Second Day',
        self::SHIPPING_OVERNIGHT => 'Next Day Air',

     * @var string
    protected $_code = 'customshipping';

     * @var bool
    protected $_isFixed = true;

     * @var MagentoShippingModelRateResultFactory
    private $rateResultFactory;

     * @var MagentoQuoteModelQuoteAddressRateResultMethodFactory
    private $rateMethodFactory;

     * @var PerfectmakeupmirrorsCustomShippingHelperData
    private $helper;

     * @var PsrLogLoggerInterface
    protected $_logger;

     * @var PerfectmakeupmirrorsPmmFedexModelPmmFedexCarrier;
    private $carrierFedex;

     * @var MagentoFrameworkAppResourceConnection;
    protected $resource;

     * @var MagentoFrameworkAppResourceConnection;
    protected $scopeConfig;

     * @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
     * @param MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory
     * @param PsrLogLoggerInterface $logger
     * @param MagentoShippingModelRateResultFactory $rateResultFactory
     * @param MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory
     * @param MagentoFrameworkAppResourceConnection $resource
     * @param array $data
    public function __construct(
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory,
        PsrLogLoggerInterface $logger,
        MagentoShippingModelRateResultFactory $rateResultFactory,
        MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory,
        PerfectmakeupmirrorsCustomShippingHelperData $helper,
        PerfectmakeupmirrorsPmmFedexModelPmmFedexCarrier $carrierFedex,
        MagentoFrameworkAppResourceConnection $resource,
        array $data = ()
    ) {
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);

        $this->rateResultFactory = $rateResultFactory;
        $this->rateMethodFactory = $rateMethodFactory;
        $this->carrierFedex = $carrierFedex;
        $this->_logger = $logger;
        $this->helper = $helper;
        $this->resource = $resource;
        $this->scopeConfig = $scopeConfig;

     * Custom Shipping Rates Collector
     * @param RateRequest $request
     * @return MagentoShippingModelRateResult|bool
    public function collectRates(RateRequest $request)
        if (!$this->getConfigFlag('active')) {
            return false;
        if ($request->getDestCountryId() == "CA") {
            // Shipping to Canada.
            // 1. Get the array of method objects from $fedex_results object
            // 2. Loop through the array of method objects to set carrier.
            // 3. Set carrier to custom shipping.
            // 4. Add brokerage to the shipping cost based on the table canada_brokerage_fees.
            $fedex_results = $this->carrierFedex->collectRates($request);

            // MagentoQuoteModelQuoteAddressRateResultMethod $methods
            $methods = $fedex_results->getAllRates();
            foreach ($methods as $method) {
                // 3. Set carrier to custom shipping.

                // Get subtotal from the $request object.
                // The request object is not giving the subtotal directly.
                // Using Quote object.
                // Refer https://magento.stackexchange.com/questions/341393/magento-2-4-2-how-to-get-order-sub-total-in-my-custom-module/341410#341410
                $quote = null;
                $items = $request->getAllItems();
                foreach($items as $item) {
                    /** @var Quote $quote */
                    $quote = $item->getQuote();
                if (!$quote) {
                    return false;
                $subtotal = $quote->getSubtotal();                

                // Convert subtotal from USD to CAD
                // Get current CAD rate.
                $current_usd_to_cad_rate = $this->get_magento_system_info("pmm/currency_conversion/usd_to_cad_conversion");
                $subtotal_cad = $subtotal * floatval($current_usd_to_cad_rate);

                // Use table canada_brokerage_fee in Magento DB
                // Create DB connection
                // http://blog.chapagain.com.np/magento-2-run-custom-sql-query/
                $connection = $this->resource->getConnection();
                $tableName = $this->resource->getTableName('canada_brokerage_fees');

                // Compute brokerage.
                // Fetch relevant fee from the table canada_brokerage_fees 
                // based on the merchandise total.
                $sql = $connection->select()->from($tableName)->where('max_merchandise_cost <= ?', $subtotal_cad)
                ->order('brokerage_cost_cad DESC')->limit(1);
                $result = $connection->fetchAll($sql);                
                $brokerage_cost_cad = $result(0)('brokerage_cost_cad') ?? 0;

                // TODO
                // Compute duty and add it to brokerage_cost_cad.
                // All mirror and skin care products are duty free.

                // Set new price with brokerage added to the shipping cost.
                $method->setPrice($method->getPrice() + $brokerage_cost_cad);
            return $fedex_results;
        } elseif (($request->getDestRegionCode() == "AK") || ($request->getDestRegionCode() == "HI") || ($request->getDestCountryId() == "PR")) {
            // For Alaska, Hawaii, and Puerto Rico
            // We want to get the shipping computation using Fedex

            $fedex_results = $this->carrierFedex->collectRates($request);
            // 1. Get the array of method objects from $fedex_results object
            // 2. Loop through the array of method objects to set carrier.
            // 3. Set carrier to custom shipping. 

            // MagentoQuoteModelQuoteAddressRateResultMethod $methods
            $methods = $fedex_results->getAllRates();
            foreach ($methods as $method) {
            return $fedex_results;

        // Below Processing is for Mainland USA.
        // Custom Shipping needs to be computed.
        /** @var MagentoShippingModelRateResult $result */
        $result = $this->rateResultFactory->create();

        /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
        $method = $this->rateMethodFactory->create();



        $this->_logger->info(__FILE__ . ': At Start');

        // Get all the items.
        // NOTE: This getAllItems here is related to Quote and not the order.
        if ((!($items = $request->getAllItems())) or (count($items) == 0)) {
            return FALSE;

        //Standard code commented. Custom code added.
        //$shippingCost = (float)$this->getConfigData('shipping_cost');
        $shippingCost = (float)$this->helper->compute_standard_shipping_cost($items);



        return $result;

     * Get all the allowed methods for Custom Shipping. This sends the CustomShipping
     * method as well as all the allowed methods of Fedex. This is required to ensure
     * the Fedex shipping methods to be considered as valid methods.
     * @return array
    public function getAllowedMethods()
        $custom_shipping_methods = ($this->_code => $this->getConfigData('name'));
        $allowed_methods = array_merge($custom_shipping_methods, $this->FEDEX_METHODS);
        return $allowed_methods;

     * get_magento_system_info - receives the path as parameter.
     * This parameter holds the path to the field.
     * This function returns a value in Magento core_config_data table.
     * @param string $path
     * @return string $config_data
    private function get_magento_system_info($path)
        // Refer https://store.magenest.com/blog/get-set-config-data-programmatically-magento-2/
        // Refer https://firebearstudio.com/blog/how-to-write-and-get-config-values-by-scope-in-magento-2.html

        // Call functions to get config data with default (global) scope
        return $this->scopeConfig->getValue($path);

ag.algebraic geometry – About condition for structure sheaf of a scheme being compact object in a category of sheaf of module over X

I found the condition for one direction :
Categorical interpretation of quasi-compact quasi-separated schemes

This article said that if $X$ is quasi compact and quasi separated, $mathscr{O}_X$ is a compact object in $Qcoh(X)$. A comment said that if the structure sheaf is compact( the global section functor preserves a colimit in $Qcoh(X)$), then $X$ is quasi compact.

Is there any result about quasi-separatedness?

magento2 – Magento 2.3.5 – Could we configure our installed module fields via Magento 2 CLI?

Could we actually configure our installed module fields via Magento 2 CLI?

For example:

# typical way to enable a Magento 2 module, via Magento 2 CLI
bin/magento module:enable Acme_Acme

# can we do something like this?
bin/magento module:enable Acme_Acme --config=[password=1234]

The goal is to make bin/magento module:enable a little dummy-proof, if some more than one ‘required’ configuration field is still null or empty.

When the module is enabled, with the empty ‘required’ configuration fields, the plugin causes Magento 2 site errors and so on.

Alternative solutions:
If going about the Magento 2 CLI is the wrong path towards a solution, what other solutions are available? Using the di.xml file? Programmatically “catch” for empty ‘required’ configuration fields?

FYI, Magento 2 casual here. Please feel free to share your thoughts, thank you.

ag.algebraic geometry – How to construct a sheaf on the infinitesimal site from a stratified module

Let $Xto S$ be a morphism of schemes.
Proposition 2.11 of the book “Notes on crystalline cohomology” by Berthelot and Ogus states that a stratified $mathcal{O}_X$-module $(E,{varepsilon_ncolon P^n_{X/S}otimes Eto Eotimes P^n_{X/S}}_n)$ gives rise to a crystal on the infinitesimal site $mathrm{Inf}_{X/S}$ (and vice versa).
However, their (sketch of) proof only treats infinitesimal thickenings $U→T $ for $Usubset X$ which locally admit a retraction.
How can we define the value $E(T)$ for an infinitesimal thickening which does not admit a retraction even locally?
Is this proposition really true?

Is it will be good to use firebase database in android app when there is need to integrate the android app with unity based module?

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

8 – Module for payments with UPI

I am working on an e-commerce website for our own store, a small startup basically with the aim of providing value for money to the customers. We are based out of India.

I am looking at integrating Payment Gateway with the website. However, the existing solutions like Razor Pay and PayU are charging around 2% of the transaction fee.

I realized that UPI payment can by-pass these commission players and we can have the site directly integrated to complete the payment from customer to the seller’s bank account without involvement of any third-party. I searched for modules that integrates with UPI, but I couldn’t find any.

This is the UPI documentation from Government of India: UPI – API and Tech Specifications.
This is the Google Pay developer’s guide: Integrating Google Pay with your site.

I don’t have much experience in writing modules from scratch. Do I have to write a module, or is there a module for this purpose?

representation theory – Constructing a surjective intertwining map onto Verma module

I want to prove the following result:

Theorem: Let $mathfrak{g}$ be a semi-simple Lie algebra, $mathfrak{h}$ be a Cartan subalgebra, $mu in mathfrak{h}$, and $W_{mu} = mathfrak{U}_{mathfrak{g}}/I_{mu}$ be the Verma module with highest weight $mu$. Suppose that $sigma_{mu}: mathfrak{g} rightarrow mathfrak{gl} left( W_{mu} right)$ be a highest weight cyclic representation with highest weight $mu$. Then, there is a surjective intertwining map $phi: V_{mu} rightarrow W_{mu}$.

Here, $mathfrak{U}_{mathfrak{g}}$ is the universal enveloping algebra of $mathfrak{g}$, and $I_{mu}$ is a left-ideal in $mathfrak{U}_{mathfrak{g}}$ generated by elements of the form $H – langle mu, H rangle cdot 1$, where $H in mathfrak{h}$, and $X in mathfrak{g}_{alpha}$, where $alpha$ is a positive root. Also, $V_{mu} = W_{mu}/U_{mu}$, where $U_{mu}$ is the space of all elements $v$ of $W_{mu}$ which do not have a $v_0$-component (where $v_0$ is a highest weight vector for $W_{mu}$), and for any collection $X_1, X_2, cdots, X_l$ of root vectors with positive roots, the vector $pi_{mu} left( X_1 right) pi_{mu} left( X_2 right) cdots pi_{mu} left( X_l right)v$ also does not have a $v_0$-component.

The representation $pi_{mu}: mathfrak{U}_{mathfrak{g}} rightarrow W_{mu}$ is given by $pi_{mu} left( t right) left( left( v right) right) = left( tv right)$, where $left( cdot right)$ denotes the equivalence class of an element in $W_{mu}$. This representation can be restricted to $mathfrak{g}$ (through the inclusion) to give a representation of $mathfrak{g}$.

To prove the result, I have tried the following:

Firstly, using the universal property of universal enveloping algebra $mathfrak{U}_{mathfrak{g}}$, we get an associative algebra homomorphism $tilde{sigma}_{mu}: mathfrak{U}_{mathfrak{g}} rightarrow mathfrak{gl} left( W_{mu} right)$ which satisfies $tilde{sigma}_{mu} left( 1 right) = I$ and $tilde{sigma}_{mu} left( Z right) = sigma{mu} left( Z right)$, for $Z in mathfrak{g}$.

Then, we define $psi: mathfrak{U}_{mathfrak{g}} rightarrow W_{mu}$ as

$$psi left( t right) = tilde{sigma}_{mu} left( t right) w_0,$$

where $w_0$ is a highest weight vector for the representation $sigma_{mu}$ of $mathfrak{g}$. Since $tilde{sigma}_{mu}$ is an associative algebra homomorphism, $psi$ is a linear map between vector spaces. Now, for $H in mathfrak{h}$, we see that

$$psi left( H – langle mu, H rangle cdot 1 right) = tilde{sigma}_{mu} left( H – langle mu, H rangle cdot 1 right) w_0 = sigma_{mu} left( H – langle mu, H rangle cdot 1 right) w_0 = 0.$$

Also, for any $X in mathfrak{g}_{alpha}$, where $alpha$ is a positive root, we have

$$psi left( X right) = tilde{sigma}_{mu} left( x right) w_0 = sigma_{mu} left( X right) w_0 = 0,$$

since $w_0$ is a highest weight vector for $sigma_{mu}$. That is, we see that $I_{mu} subseteq ker psi$. Hence, this map descends to a linear map $tilde{psi}: W_{mu} = mathfrak{U}_{mathfrak{g}}/I_{mu} rightarrow W_{mu}$.

Now, I want to prove that $U_{mu} subseteq ker tilde{psi}$ so that it can descend to a map $phi: V_{mu} rightarrow W_{mu}$. However, I cannot prove it! If we start with any $v in U_{mu}$, then it does not have a $v_0$-component. We want that $tilde{psi} left( v right) = 0$. This would mean that $tilde{sigma}_{mu} left( v right) w_0 = 0$. However, I cannot see why this should happen?

Is there something wrong in this attempt? Any help will be appreciated!

how to create custom module to remove .html part from product page?

I want to create a custom module to remove .html part from product page. I have found solution only from admin panel but want editing in module.

DreamProxies - Cheapest USA Elite Private Proxies 100 Private Proxies 200 Private Proxies 400 Private Proxies 1000 Private Proxies 2000 Private Proxies 5000 Private Proxies ExtraProxies.com - Buy Cheap Private Proxies Buy 50 Private Proxies Buy 100 Private Proxies Buy 200 Private Proxies Buy 500 Private Proxies Buy 1000 Private Proxies Buy 2000 Private Proxies ProxiesLive.com Proxies-free.com New Proxy Lists Every Day Proxies123.com Buy Cheap Private Proxies; Best Quality USA Private Proxies