Skip to content

Instantly share code, notes, and snippets.

@PatrickTC
Last active March 22, 2018 13:47
Show Gist options
  • Save PatrickTC/68a8294983876db426c4e333ff28fcbf to your computer and use it in GitHub Desktop.
Save PatrickTC/68a8294983876db426c4e333ff28fcbf to your computer and use it in GitHub Desktop.
Drupal 8 stuff that's seemingly impossible to remember and FUN!
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
\Drupal::logger('your_lame_module')->notice('@type: %title.',
array(
'@type' => "Massive Error",
'%title' => "<pre>".print_r($error_array_or_text, true)."</pre>",
));
<?php
use Symfony\Component\HttpFoundation\RedirectResponse;
$redirect = new RedirectResponse(Url::fromRoute('cat_module.route_name', ['route_arg' => 'a_value'])->toString());
$redirect->send();
/**
* 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);
}
/**
* 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;
}
}
}
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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment