$url = Url::fromRoute($route_name);
$link = Link::fromTextAndUrl($text, $url);
$link = Link::createFromRoute('Link Text', 'route_name')->getUrl();
\Drupal::service('page_cache_kill_switch')->trigger();
UrlHelper::stripDangerousProtocols($url);
$css_class = Html::cleanCssIdentifier('css_identifier');
function mymodule_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
$field_definition = $context['items']->getFieldDefinition();
if ($field_definition->getType() == 'path' && isset($element['alias'])) {
$element['alias']['#element_validate'] = array('_mymodule_field_widget_validate_element');
}
}
function _mymdule_field_widget_validate_element(array &$element, FormStateInterface $form_state) {
$value = rtrim(trim($element['#value']), " \\/");
if (!empty($value)) {
// Validate stuff and throw error
// $form_state->setError($element, t('some error'));
}
}
$active_link = \Drupal::service('menu.active_trail')->getActiveLink();
use Drupal\taxonomy\Entity\Term;
Term::load($id);
$terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vocabulary);
$storage = \Drupal::service('entity_type.manager')
->getStorage('storage_type');
$storage->resetCache();
$entity = $storage->load($entity_id);
$entity = \Drupal::service('entity.repository')
->loadEntityByUuid($bundle, $uuid);
\Drupal::service('module_installer')->install(array('my_module'));
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
$fields['reference'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Entity reference'))
->setDescription(t('Entity reference'))
->setRevisionable(TRUE)
->setSetting('target_type', 'entity_type')
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);