Drupal Dump

Drupal Snippets, Hacks and more....

413 posts since 2011

Drupal commerce how to add Payment programmatically

Payment is specific entity in drupal commerce, usually it will be added by payment gateway module through checkout flow, but if you need to make some custom payments programmatically then you can do it in a following way, first you would need to get payment gateway object, so you can fetch proper info, after that you need to fill in paraparameters for new payment creation, depending on how you

Drupal commerce how to make shipments(shipping) programmatically

Drupal commerce shipping module is complex, there are few moving parts that you need to consider when making custom changes to it. First there are shipment types which are associated with order type, this is usually done through UI, unless you also add order type programmatically.

Setting Ngrok with Docker or Vagrant

Ngrok is great tool, to set it up to work with docker and your specific localhost you need to have account, make authtoken command and then run following to receive both http and https versions of your local environment exposed to internet.

How to swap class for drupal 8 services

One of the reasons we are using services instead of plain classes is possibility to swap out class that each service uses.  So what we need to do is we need to create a new class that's overriding an existing service class, and then we need to alter the service container to use our new class.

Jquery once in drupal 8

Jquery is still standard part of drupal core and when using JavaScript or Ajax you will probably use Jquery and Jquery once function. Code can be found in core/assets/vendor/jquery-once/jquery.once.js  so you could look how it works. It is pretty similar as in D7, look at the comments on how to use it.

Swapping out classes in plugins

In drupal 8 plugins are everywhere, so to get most out of them you will sometimes want to alter them. If you look at definitions of each plugin manager, you will see that all of them have something specific in their constructors, it is the line below

When entity get returns array, do this

You are using get with fieldname and you are getting array as result, to get value as string, use getString() method like below

$entity->get("fieldName")->getString()