ddedic
3/22/2018 - 1:47 PM

Drupal 8 stuff that's seemingly impossible to remember and FUN!

Drupal 8 stuff that's seemingly impossible to remember and FUN!

State API is AWESOME because its super handy!

    Get a value:
    $val = \Drupal::state()->get('key');
     
    Get multiple key/value pairs:
    $pairs = \Drupal::state()->getMultiple($keys);
     
    Set a value:
    \Drupal::state()->set('key','value');
     
    Set multiple values:
    \Drupal::state()->setMultiple($keyvalues);
     
    Delete a value:
    \Drupal::state()->delete('key');
/**
 * Implements hook_views_pre_view().
 */
function HOOK_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  if ($view->id() == 'view_id') {
    switch ($display_id) {
      case 'block_1':
        // Get exposed filters value
        $filter_input = $view->getExposedInput();
        // Field transfer data area.
        $filter_input['field_b'] = $filter_input['field_a'];
        $view->setExposedInput($filter_input);
        break;
    }
  }
}
/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @param $form
 * @param FormStateInterface $form_state
 * @param $form_id
 */
function MODULE_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Add another submit handler
  $form['#submit'][] = '_MODULE_user_login_form_submit';
}


/**
 * Redirect submit handler. We want the user to be redirected to the previous page after login
 *
 * @param $form
 * @param FormStateInterface $form_state
 */
function _MODULE_user_login_form_submit($form, FormStateInterface $form_state) {
  $previousUrl = \Drupal::requestStack()->getParentRequest()->getUri();
  $response = new TrustedRedirectResponse($previousUrl);
  $form_state->setResponse($response);
}
<?php
use Symfony\Component\HttpFoundation\RedirectResponse;

$redirect = new RedirectResponse(Url::fromRoute('cat_module.route_name', ['route_arg' => 'a_value'])->toString());
$redirect->send();
<?php 
\Drupal::logger('your_lame_module')->notice('@type:  %title.',
        array(
            '@type' => "Massive Error",
            '%title' => "<pre>".print_r($error_array_or_text, true)."</pre>",
        ));

Herein lies horrors of the likes that no one has ever encountered before. 
Take a deep breath and scroll down to reveal the code crapiness (or 1337ness) that will help one circumnavigate the open code ocean that is Drupal 8, 9, 10.. etc.
<?php

// Shamelessly borrowed from https://gist.github.com/r-daneelolivaw/b420b3dc0c40a6cacf76

// Autoload Url and Link classes.
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
 * External link
 */
$url = Url::fromUri('https://colorfield.be');
$link = Link::fromTextAndUrl(t('Colorfield'), $url);
$link = $link->toRenderable();
$link['#attributes'] = array('class' => array('external'));
$output = render($link); 

/**
 * Internal route
 */
$url = Url::fromRoute('contact.site_page'); // a route provided in .routing.yml
$link = Link::fromTextAndUrl(t("Contact"), $url);
$link = $link->toRenderable();
$link['#attributes'] = array('class' => array('internal'));
$output = render($link);

/**
 * Entities, e.g. node
 * @see http://stackoverflow.com/questions/35397009/creating-a-link-from-node-id-in-drupal-8
 */
$options = ['absolute' => TRUE];
$url = Url::fromRoute('entity.node.canonical', ['node' => 1], $options);
//$url = $url->toString();
$link = Link::fromTextAndUrl("Node title", $url);
$link = $link->toRenderable();
$link['#attributes'] = array('class' => array('internal'));
$output = render($link);

/**
 * Internal path
 */
$path = '/my/path'; // prefixed with / 
$url = Url::fromUri('internal:'.$path);
$link = Link::fromTextAndUrl($label, $url);
$link = $link->toRenderable();
$link['#attributes'] = array('class' => array('internal'));
$output = render($link); 

/**
 * Anchor (current page)
 */
$url = Url::fromRoute('<current>', array(), array('fragment' => $name));
$link = Link::fromTextAndUrl($label, $url);
$link =  $link->toRenderable(); 
$output = render($link);