Are you a drupal developer then this tutorial is very useful for you. In this tutorial, You will find some extremely useful code snippets for your drupal 8 site. I have collected these all snippets from google and create a complete tutorial in one place for my site visitors. These snippets will help you to add some extra features in your drupal websites.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | < ? /** * Implements hook_menu(). */ function my_module_menu() { $items['admin/my-module'] = array( 'title' => 'My module admin page', 'page callback' => 'system_admin_menu_block_page', 'access arguments' => array('access administration pages'), 'file path' => drupal_get_path('module', 'system'), 'file' => 'system.admin.inc', ); $items['admin/my-module/settings'] = array( 'title' => 'Basic Settings', 'description' => 'My module Settings', 'page callback' => 'drupal_get_form', 'page arguments' => array('my_module_settings_form'), 'access arguments' => array('access my module settings page'), ); return $items; } |
Use below code to get custom user fields in drupal 8.
1 2 3 4 5 | function theme_preprocess_node(&$variables){ $node = $variables['node']; $variables['firstname'] = $node->getOwner()->field_first_name->value; $variables['lastname'] = $node->getOwner()->field_last_name->value; } |
Below function will calculate the difference between the current date and given date.
1 2 3 4 5 6 7 8 9 10 11 12 13 | function get_time_diff_in_minutes($time_given) { if ($time_given != '') { $time = time(); $current_time = date('Y-m-d H:i:s', $time); $current_time_object = new DateTime($current_time); $time_given_object = new DateTime($time_given); $interval = date_diff($current_time_object, $time_given_object); $minutes = $interval->days * 24 * 60; $minutes += $interval->h * 60; $minutes += $interval->i; return $minutes; } } |
Are you want to create a user account programmatically in drupal 8. Below drupal code will create a user account programmatically in your drupal 8. Copy below function and use it in your web page to create a user account in your drupal 8 websites.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public function submitForm(array &$form, FormStateInterface $form_state) { $language = \Drupal::languageManager()->getCurrentLanguage()->getId(); $user = \Drupal\user\Entity\User::create(); $user->setPassword($form_state->getValue('pass')); $user->enforceIsNew(); $user->setEmail($form_state->getValue('mail')); $user->setUsername($form_state->getValue('name'));//This username must be unique and accept only a-Z,0-9, - _ @ . $user->set("init", 'mail'); $user->set("langcode", $language); $user->set("preferred_langcode", $language); $user->set("preferred_admin_langcode", $language); $user->activate(); //Save user account $user->save(); // No email verification required; log in user immediately. _user_mail_notify('register_no_approval_required', $user); user_login_finalize($user); drupal_set_message($this->t('Registration successful. You are now logged in.')); $form_state->setRedirect(''); } |
1 2 3 4 5 6 7 8 9 | //render a link $url = Url::fromRoute('modaltest.page'); $internal_link = \Drupal::l(t('Modal Page'), $url); // Other examples $internal_link = \Drupal::l(t('Modal Page'), Url::fromRoute('modaltest.page')); // Return an link. return array('#markup' => \Drupal::l(t('Modal Page'), Url::fromRoute('modaltest.page'))); |
1 2 | $permissions = \Drupal::service('user.permissions')->getPermissions(); print_r($permissions); |
Output will be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | Array ( [administer blocks] => Array ( [title] => stdClass Object ( [__CLASS__] => Drupal\Core\StringTranslation\TranslatableMarkup [string] => Administer blocks [translatedMarkup] => [options] => Array ( ) [stringTranslation] => stdClass Object ( [__CLASS__] => Drupal\Core\StringTranslation\TranslationManager [translators] => Array ( [30] => Array ( [0] => Drupal\Core\StringTranslation\Translator\CustomStrings ) ) [sortedTranslators] => Array ( [0] => stdClass Object ( [__CLASS__] => Drupal\Core\StringTranslation\Translator\CustomStrings [settings] => Drupal\Core\Site\Settings [translations] => Array(1) ) ) [defaultLangcode] => en ) [arguments] => Array ( ) ) [description] => [provider] => block ) [export configuration] => Array ( [title] => stdClass Object ( [__CLASS__] => Drupal\Core\StringTranslation\TranslatableMarkup [string] => Export configuration [translatedMarkup] => [options] => Array ( ) [stringTranslation] => stdClass Object ( [__CLASS__] => Drupal\Core\StringTranslation\TranslationManager [translators] => Array ( [30] => Array ( [0] => Drupal\Core\StringTranslation\Translator\CustomStrings ) ) [sortedTranslators] => Array ( [0] => stdClass Object ( [__CLASS__] => Drupal\Core\StringTranslation\Translator\CustomStrings [settings] => Drupal\Core\Site\Settings [translations] => Array(1) ) ) [defaultLangcode] => en ) [arguments] => Array ( ) ) [restrict access] => 1 [description] => [provider] => config ) [import configuration] => Array ( [title] => stdClass Object ( [__CLASS__] => Drupal\Core\StringTranslation\TranslatableMarkup [string] => Import configuration [translatedMarkup] => [options] => Array ( ) [stringTranslation] => stdClass Object ( [__CLASS__] => Drupal\Core\StringTranslation\TranslationManager [translators] => Array ( [30] => Array ( [0] => Drupal\Core\StringTranslation\Translator\CustomStrings ) ) [sortedTranslators] => Array ( [0] => stdClass Object ( [__CLASS__] => Drupal\Core\StringTranslation\Translator\CustomStrings [settings] => Drupal\Core\Site\Settings [translations] => Array(1) ) ) [defaultLangcode] => en ) [arguments] => Array ( ) ) [restrict access] => 1 [description] => [provider] => config ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * Implements hook_css_alter(); */ function MY_THEME_css_alter(&$css) { $remove = array( 'misc/vertical-tabs.css', 'misc/vertical-tabs-rtl.css', 'misc/ui/jquery.ui.core.css', 'misc/ui/jquery.ui.theme.css', 'modules/comment/comment.css', 'modules/comment/comment-rtl.css', 'modules/field/theme/field.css', 'modules/field/theme/field-rtl.css', 'modules/file/file.css', 'modules/filter/filter.css', 'modules/node/node.css', 'modules/search/search.css', 'modules/search/search-rtl.css', 'modules/system/system.admin.css', 'modules/system/system.admin-rtl.css', 'modules/system/system.base.css', 'modules/system/system.base-rtl.css', 'modules/system/system.maintenance.css', 'modules/system/system.menus.css', 'modules/system/system.menus-rtl.css', 'modules/system/system.messages.css', 'modules/system/system.messages-rtl.css', 'modules/system/system.theme.css', 'modules/system/system.theme-rtl.css', 'modules/user/user.css', 'modules/user/user-rtl.css', 'sites/all/modules/ctools/css/ctools.css', ); foreach ($remove as $value) { unset($css[$value]); } } |
1 2 3 4 5 6 7 | /** * Implements hook_file_insert(). */ function MYMODULE_file_insert($file) { $hash = 'public://' . md5($file->filename) . '.' . pathinfo($file->filename, PATHINFO_EXTENSION); file_move($file, $hash, 'FILE_EXIST_REPLACE'); } |
1 2 3 4 | $block_id = 1; $block = \Drupal::entityManager()->getStorage('block_content')->load($block_id); $block_view = \Drupal::entityManager()->getViewBuilder('block_content')->view($block); $variables['variable_name'] = $block_view; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | namespace Drupal\example\Form; use Drupal\Core\Form\FormBase; class ExampleForm extends FormBase { /** * Implements \Drupal\Core\Form\FormBase::buildForm(). */ public function buildForm(array $form, array &$form_state) { // Define your form here, as usual. $form[’example_form_email'] = array( '#type' => ’email', '#title' => t(‘Please enter your email address.'), ); return $form; } /** * Implements \Drupal\Core\Form\FormBase::submitForm(). */ public function submitForm(array &$form, array &$form_state) { $example_email = $form_state['values']['example_form_email’]); config(‘example_form.email') ->set('example_email', $example_email) ->save(); } } |
Hope this tutorial hep you. Do you like & share this article with your friends, and don’t forget to follow us on Facebook and Twitter to learn cool Drupal tutorials.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: drupal snippets