Skip to content

Instantly share code, notes, and snippets.

@steffenr
steffenr / select2_behat_feature_context.php
Last active September 13, 2018 12:24
Behat / Select2 Feature context
<?php
/**
* @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
*/
public function iFillInSelectInputWithAndSelect($field, $value, $entry)
{
$page = $this->getSession()->getPage();
$inputField = $page->find('css', $field);
if (!$inputField) {
@steffenr
steffenr / route_name.php
Last active July 1, 2017 21:53
Get current route name based on request in drupal 8.
<?php
$route_name = \Drupal::service('current_route_match')->getRouteName();
?>
@steffenr
steffenr / expose_author_uid.php
Created January 30, 2017 07:13
[Drupal8] make user field configurable in comment
<?php
/**
* Implements hook_entity_base_field_info_alter().
*/
function mymodule_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
switch ($entity_type->id()) {
case 'comment':
$fields['uid']->setDisplayConfigurable('view', TRUE);
break;
@steffenr
steffenr / gist:84ba5a5c8bd5af91e20d496e16f05eb3
Created December 12, 2016 10:48
Determine the size of MySQL databases
SELECT table_schema AS "Database",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)"
FROM information_schema.TABLES
GROUP BY table_schema;
@steffenr
steffenr / .htaccess
Created June 15, 2012 10:05
Drupal htaccess optimizations
# KILL THEM ETAGS
# http://www.askapache.com/htaccess/apache-speed-etags.html
FileETag none
# set expires header
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf|woff|eot|svg|ttf)$">
Header set Expires "Tue, 16 Jun 2020 20:00:00 GMT"
</FilesMatch>
# turn on gzip compression
@steffenr
steffenr / mymodule.module
Created August 15, 2016 10:01
Hide fieldset relations in taxonomy term forms for flat hierarchy
<?php
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_taxonomy_my_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Hide fieldset relations for flat hierarchy taxonomies.
$form['relations']['#required'] = FALSE;
$form['relations']['#access'] = FALSE;
}
@steffenr
steffenr / pseudo_field_node_edit.php
Last active August 3, 2016 18:19
Add pseudo field to node edit form (works fine in combination with field_group module)
<?php
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_id == 'node_article_edit_form') {
$form['my_view'] = [
'#markup' => 'Some arbitrary markup.',
];
// Access entity object.
@steffenr
steffenr / url.php
Last active March 24, 2016 10:51
Create link with attributes in Drupal 8
<?php
$link_url = Url::fromRoute('node.add', array('node_type' => $content_type));
$link_url->setOptions(array(
'attributes' => array(
'class' => array('button')
),
'query' => \Drupal::destination()->getAsArray(),
));
$link_text = t('Add !content_type content', array('!content_type' => ucfirst($content_type)));
// Build link
@steffenr
steffenr / cleanup.sh
Last active March 21, 2016 13:56
Clean up all git branches except 'develop'
git branch | grep -v "develop" | sed 's/^[ *]*//' | sed 's/^/git branch -d /' | bash
@steffenr
steffenr / category_marker.php
Last active January 3, 2016 12:19
Use hook_leaflet_views_alter_points_data_alter to add category based icon to your markers shown with leaflet_views.
<?php
/**
* Implements hook_leaflet_views_alter_points_data_alter().
*/
function fewo_map_leaflet_views_alter_points_data_alter($result, &$points) {
// Get node object of result.
$wrapped_node = entity_metadata_wrapper('node', $result->nid);
// Check if we have a category attached to node.
$category = $wrapped_node->field_sight_category->value();
if (!empty($category)) {