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'].
public function buildRow(EntityInterface $entity) {
$row['active']['data'] = [ // @IMPORTANT: add ['data'] here
'#type' => 'checkbox',
'#default_value' => (bool) $entity->isActive(),
'#checked' => (bool) $entity->isActive(),
'#attributes' => ['disabled' => 'disabled'],
'#disabled' => TRUE,
];
return $row + parent::buildRow($entity);
}
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.
Check error logs at /var/log/apache2/error.log
sudo tail -f /var/log/apache2/error.log
Configuration | Performance - Check off Aggregate CSS files and Aggregate JavaScript files
Configuration | Performance - Clear all caches
Rebuild cache from Drush
# Move to the directory where Drupal 8 is installed by using cd command,
sudo drush -l <YOUR_SITE_URL> cr
Download and install a Bootstrap theme. Modify the following at
vi bootstrap/templates/menu/system/form-element.func.php
/**
* Overrides theme_form_element().
*/
function bootstrap_form_element(&$variables) {
$element = &$variables['element'];
$name = !empty($element['#name']) ? $element['#name'] : FALSE;
$type = !empty($element['#type']) ? $element['#type'] : FALSE;
$checkbox = $type && $type === 'checkbox';
$radio = $type && $type === 'radio';