Drupal Dump

Drupal Snippets, Hacks and more....

How to put form in a block programmaticaly

Putting some form in a block is matter of creating a new block and assigning a form to it through form_builder that is called through dependency injection(DI) and then using method getForm that will get rendered array of this form, which is exactly what build method for block expects to output

Event subscribers and listeners, the short version

In drupal 8 we have cool new thing, instead of hooks we can use event subscribers and create event listeners. They do similar things as hooks, we can do something on certain "events" in system, get data and change it or somehow act upon it.

So to subscribe to some event we need to do a service with tags in it, like

Get entity object in form alter

If you need to get entity object in form alter, you can get it with this method

$entity = $form_state->getFormObject()->getEntity();

and form then you can get info you need, use methods on it etc

Add GIT branch to terminal prompt, works in your IDE also (PHPSTORM)

# Git branch in prompt.

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

So open ~/.bash_profile in your favorite editor and add the above content to the bottom.