Skip to content

Instantly share code, notes, and snippets.

View crittermike's full-sized avatar

Mike Crittenden crittermike

View GitHub Profile
@crittermike
crittermike / terms.php
Created November 14, 2016 21:40
Load terms in a taxonomy in Drupal 8
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('categories');
if (!empty($terms)) {
foreach($terms as $term) {
// do whatever with $term here.
}
}
@crittermike
crittermike / ordinal.php
Created November 3, 2016 18:18
Simple function for formatting a number as an ordinal in PHP
<?php
/**
* Format number as ordinal.
*
* @param string $number
* The unformatted number.
*
* @return string
* The ordinal number.
@crittermike
crittermike / drush.sh
Created November 2, 2016 14:15
Install Drush on Cloud9
composer global require drush/drush:7.*
# Now add ~/.composer/vendor/bin to your $PATH in ~/.bashrc, like so:
# export PATH="/home/ubuntu/.composer/vendor/bin:$PATH"
@crittermike
crittermike / gist.php
Last active October 29, 2016 07:35
Using a custom field template from inside a module in Drupal 8
<?php
/**
* Implements hook_theme().
*
* Note that this isn't necessary if the template is in the site theme rather than a module.
* See https://www.drupal.org/docs/8/theming/twig/twig-template-naming-conventions
*/
function my_module_theme($existing, $type, $theme, $path) {
return array(
@crittermike
crittermike / IntegerDropdownWidget.php
Created October 17, 2016 20:14
Drupal 8 form widget example: creates a select dropdown for integer fields. Place in src/Plugin/Field/FieldWidget
<?php
/**
* @file
* Defines a dropdown widget for integer fields.
*/
namespace Drupal\nba_content_core\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FieldItemListInterface;
@crittermike
crittermike / xhprof.sh
Last active October 7, 2016 17:10
Installing XHProf on Ubuntu 14.04 (for Cloud9)
sudo apt-get update
sudo apt-get install php-pear php5-dev php5-mcrypt
sudo php5enmod mcrypt
sudo pecl install xhprof-beta
echo "extension=xhprof.so" > /tmp/xhprof.ini
sudo mv /tmp/xhprof.ini /etc/php5/mods-available/
sudo php5enmod xhprof
sudo service apache2 restart
# Now run `php --ri xhprof` to confirm that the install worked.
@crittermike
crittermike / router_rebuild.sh
Created September 16, 2016 15:15
Rebuild Drupal 8 routing table with Drush
drush ev '\Drupal::service("router.builder")->rebuild();'
@crittermike
crittermike / get_node_by_path.php
Created September 16, 2016 15:15 — forked from thagler/get_node_by_path.php
Drupal 8 Get node ID by path alias
<?php
$path = \Drupal::service('path.alias_manager')->getPathByAlias('/this-is-the-alias');
if(preg_match('/node\/(\d+)/', $path, $matches)) {
$node = \Drupal\node\Entity\Node::load($matches[1]);
}
@crittermike
crittermike / current_controller.php
Created September 15, 2016 18:45
How to call a method of the controller handling the current request in Drupal 8
<?php
$request = \Drupal::request();
$controller_resolver = \Drupal::getContainer()->get('controller_resolver');
$controller = $controller_resolver->getController($request);
if (!empty($controller)) {
$controller_instance = $controller[0];
// Now you can call things like this:
@crittermike
crittermike / ordinal.php
Created September 15, 2016 14:26
Format a number ordinal in PHP (i.e., 1st, 2nd, 3rd, etc.)
<?php
$formatter = new NumberFormatter('en_US', NumberFormatter::ORDINAL);
print $formatter->format($number);