Snippets

HFC Web Development Drupal 8 Replacements for Common Drupal 7 Functions

Created by Micah Webner last modified

format_date()

Deprecated

see Drupal\Core\Datetime\DateFormatter::format()

\Drupal::service('date.formatter')->format($timestamp, $type, $format, $timezone, $langcode);

l()

Deprecated

see Drupal::l()

user_load()

$user = Drupal\user\Entity\User::load(1); Drupal::currentUser()->setAccount($user);

global $user

$uid = \Drupal::currentUser()->id();
$name = \Drupal::currentUser()->getAccountName();

user_access('permission name')

see also: Drupal\Core\Access\AccessResult

if ($account->hasPermission('permission name')) {}

variable_get('variable_name', 'default_value')

$setting_name = \Drupal::config('modulename.settings')->get('setting_name');

node_load()

$node = \Drupal\node\Entity\Node::load($nid);

Render a field.

See https://www.drupal.org/node/2208327

$output = $node->field_foo->view($view_mode);
$output = $node->field_foo->view(array('type' => 'some_formatter', 'settings' => array(...)));

$output = $node->field_foo[0]->view('full');
$output = $node->field_foo[0]->view(array('type' => 'some_formatter', 'settings' => array(...)));

$output[] = $entity->campus_phone->view("campus_phone_formatter")[0][0]["#markup"];

watchdog('module_name', 'message %id', ['%id' => 'value'])

\Drupal::logger('module_name')->error('message %id', ['%id' => 'value']);

db_query() and db_select()

first try to use \Drupal::entityQuery()

Can also use \Drupal::database()->query() and \Drupal::database()->select()

drupal_http_get($path, $options)

See also: GuzzleHttp\RequestOptions See also: https://api.drupal.org/api/drupal/vendor%21guzzlehttp%21psr7%21src%21Response.php/class/Response/8.2.x

$response = \Drupal::httpClient()->request('GET', $url, $options);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.