Drupal 8: How to Get the Site Hostname
\Drupal::request()->getHost()
This one doesn't include the URL that begins with https://〜, therefore you may want to write it as follows:
$hostname = sprintf('https://%s/', \Drupal::request()->getHost());
\Drupal::request()->getHost()
This one doesn't include the URL that begins with https://〜, therefore you may want to write it as follows:
$hostname = sprintf('https://%s/', \Drupal::request()->getHost());
Drupal 8 provides a useful helper funciton $entity->toLink(..). You must return it by converting to a string by using ->toString() at the end.
public function foo(Request $request = NULL) {
$id = $request->get('id'); // from query string
$entity = YourEntity::load($id);
$message = $this->t('Check at @url.', [
'@url' => $entity->toLink('<TEXT_TO_LINK_HERE>', 'edit-form')->toString(),
]);
return [
'#type' => 'markup',
'#markup' => $message,
];
}
For example shown below, ['data'] requires after ['active'] like ['active']['data'].
You can output the current date and time as follows:
print \Drupal::service('date.formatter')->format((new \DateTime())->getTimeStamp(), 'short');
Call the following static method\Drupal::service('uuid')->generate()
$uuid = \Drupal::service('uuid')->generate();
Put an Email address into $email that needs to be validated.
\Drupal::service('email.validator')->isValid($email);
$uid = \Drupal::currentUser()->id() // if you wan to get a username of the current user
$account = \Drupal\user\Entity\User::load($uid); // pass your uid
$username = $account->getUsername();
If you want to create a module that provides your custom block(s) in Drupal 8, for instance, you can only write two source codes such as my_custom_block.info.yml and MyCustomBlock.php. However how can you make custom configuration fields on your custom block translatable? The answer is to add one additional file my_custom_block.yml as a schema file.
I think we should doubt cache since Drupal 8 aggressively uses cache-backend mechanism. If you encounter a fatal error or WSOD (White Screen of Death), check the following items.
Download and install a Bootstrap theme. Modify the following at
vi bootstrap/templates/menu/system/form-element.func.php