Create a block programatically in Drupal 8

​​block_example.PNG

Basic creation of a block in Drupal 8. As seen in the image on the left, a predefined library structure is needed to create a block plugin.

The annotations plugin is used to handle the block id, subject and admin label from within the comment block. The annotation class is found at core/lib/Drupal/Component/Annotation/Plugin.php.

Reinstall Drupal 8

Reinstalling a Drupal 8 site can't be bruteforced as in Drupal 7 where dropping all tables in the database is enough to reinstall a site.

One of the reasons why just dropping tables in the database isn't enough is that the config directory (e.g. sites/default/files/config_somehashkey) needs to be deleted.

The fastest way to get nids in a default no cache situation

In this test case scenario a function was made to show the differences when querying for node ids. Mostly this kind of query is the first step to render nodes in a custom made view. Each query returns the same array of node ids. Execution times are tested by using the Drupal 7 bootstrap timer functions.

Delete all commerce products

The commerce_product_delete_multiple function can be used to bulk delete products. Similar to other ENTITY_delete_multiple functions, an array of entity ids is passed as an argument.
Drupal_set_time_limit is used to override the default 30 seconds that a script is allowed to run.

Delete all commerce orders

The commerce_order_delete_multiple function can be used to bulk delete orders. Similar to other ENTITY_delete_multiple functions, an array of entity ids is passed as an argument.
Drupal_set_time_limit is used to override the default 30 seconds that a script is allowed to run.

Check if taxonomy term exists and save the term if it doesn't

A dynamic query can be used to check if a taxonomy term exists.

Query conditions are the taxonomy term name and the vocabulary.

Create a form


/**
 * Implements hook_form().
 */
function MY_CUSTOM_FORM_form($node, &$form_state) {
  $form = array();
   
  $form['CUSTOM_FIELD'] = array(
    '#type' => 'textfield',
    '#label' => 'MY_LABEL',
  );
   
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit')
  )
   
  return $form;
}
 
/**
 * MY_CUSTOM_FORM validation handler.
 */
function MY_CUSTOM_FORM_form_validate($node, &$form_state) {
   
}
 
/**
 * MY_CUSTOM_FORM submit handler.
 */
function MY_CUSTOM_FORM_form_submit($node, &$form_state) {
   
}

Combine array values into a string


// Testing variable to check conditions.
$check = false;

$colors = array('red', 'white', 'green');
$sizes = array('small', 'medium', 'large');
$additionals = array('print', 'no print');
$combinations = array();

foreach($colors as $color) {
  foreach($sizes as $size) {
    // Add third level depending on the $check variable.
    if($check === true) {	
  	  foreach($additionals as $additional) {
	    $combinations[] = $color . '-' . $size . '-' . $additional;
	  }
	} else {
	  $combinations[] = $color . '-' . $size;	
	}
  }
}

print_r($combinations);

Pages

Subscribe to Dribbit.eu RSS