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()

 

How to xdebug hook_views_data_alter()

Lets first talk about what does hook_views_data_alter() do in first place. Views is a query editor, it queries DB, so this hook alters how our data is queried. As in views UI you add, fields, filters, sorts, relationships, here you will have an exposure of all DB tables you can make queries and data on what to use for fields, filters and much more.

How to get usable UI for translating your custom forms

This one is one of the bigger surprises in drupal 8. Form building is straight forward, basic OOP bases on symfony, but then you want your forms to be translatable, you are in for a ride. First you will need to have schema yaml files in install and schema directory.

How to get currency symbol from order object

Currency symbol is not stored in order, so to get it, you need to dig a bit deeper and fetch currency object.
I am assuming you have order stored in $this->order so then we get currency code and with that we load commerce_currency through entity type manager and use getSymbol method to get the symbol.

How to get coupon entity from order

If you wan to get coupon entity from order there is a quick magic method to do that. First load order by some property, we can use ID
 

Find the class responsible for a particular field type

$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
$class = $field_type_manager->getDefinition('entity_reference')['class'];

this will show you info 

Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem

so you can check there settings and code for this field type.