Hot answers tagged

9

As of Drupal 8.8, path aliases are now entities, and the method of creating a new alias has changed. To programmatically save an alias, you can do the following: $path_alias = \Drupal\path_alias\Entity\PathAlias::create([ 'path' => '/unaliased/path', 'alias' => '/aliased/path', ]); $path_alias->save();


8

You can using like jQuery ajax. Because Drupal.ajax({}).execute() is a function and return the jqXHR. /** * Execute the ajax request. * * Allows developers to execute an Ajax request manually without specifying * an event to respond to. * * @return {object} * Returns the jQuery.Deferred object underlying the Ajax request. If * ...


8

Now Drupal recommended project supports version as command line option. If you want to install version 8.8.5 via Composer then command will like as shown below. composer create-project drupal/recommended-project:8.8.5 <INSTALL_DIR>


8

That's Drupal's Composer Scaffold. Listed as "drupal/core-composer-scaffold": "^8.8.0" or a different version in your project's composer.json file. To prevent certain scaffold files from being overwritten every time you run a Composer command you need to specify them one by one in the "extra" section of your project's composer.json. See the docs on ...


8

The best practice is still to use Drupal's behaviors system even when not relying on jQuery. See Drupal 8 Drupal.behaviors without jquery for reference. That said, I think you're on the right path. I find jQuery just slows down the page load when vanilla JS will usually do the trick. That's just my opinion and there's a lot of debate out there.


7

First, it's probably preferable to use the provider option if you can. However, Field widgets and formatters also make use of an isApplicable() method. You can use this for any additional logic to determine whether your widget should be available for a field, a field in the subset of fields defined in your annotation. You can use this method to limit the ...


7

Yes, this is possible. You may declare a dependency on a module by using the "provider" key in the annotation. For your example, this would look like: /** * @FieldWidget( * id = "my_daterange", * label = @Translation("Date and time range + All day"), * field_types = { * "daterange" * }, * provider =...


7

You can use the Metatag module. It has a section for front page metatags, where you can set the title.


7

As @No Sssweat recommend, the D8 Migrate Modules is good way. With your way, you have to add property pre_hashed for field pass. $values = [ 'name' => 'test', 'mail' => 'test@test.com', 'roles' => [], 'pass' => [ 'value' => $hashed_drupal_7_password, 'pre_hashed' => TRUE, ], 'status' => 1, ]; $account = entity_create(...


7

renderRoot() is for rendering the final result returned by a controller. renderPlain() is for rendering HTML which is not part of a page and is used for other purposes like emails. In your case the correct method would be render(), because this is the only render method which can bubble up metadata to the page. But you don't need to render at all, because ...


7

From the EU Cookie Compliance module's project page on drupal.org: If you want to conditionally set cookies in your module, there is a JavaScript function provided that returns TRUE if the user has given their consent: Drupal.eu_cookie_compliance.hasAgreed() For example, if you want to disable Google Analytics, you could do the following: http://drupal.org/...


7

This command will fix it temporarily: chmod u+w web/sites/default But Drupal will harden permission again soon. To turn it off add this to settings.php: $settings['skip_permissions_hardening'] = TRUE; Source: https://www.drupal.org/docs/develop/using-composer/starting-a-site-using-drupal-composer-project-templates#s-troubleshooting-permission-issues-...


6

Generally speaking, a view is used to show data for a list of entities. View filters are used to select (or filter) which entities are handled by the view; contextual filters are used to select the entities basing on the values passed in the URL. For example, the Files view that comes with Drupal 8 has two pages: one whose path is /admin/content/files and ...


6

Then don't use custom block types because they are stored in content entities. Use blocks storing only configuration. You can build a custom block plugin, even with a body field. See Custom block build function to display wysiwyg body text There are also some contrib options like Simple Block which is implementing a config entity for blocks.


6

You just seem to be getting confused with the form build concept and PHP logic - what you need to do is add to the existing form array ($form in buildForm), not create a second separate form/array. With that in mind, this: $this->secondForm($param); Doesn't do anything. You're not merging the return value back into $form, and you're not passing $form to ...


6

Create a custom content entity with entity_reference fields relating each "email" entity to a node and a user. The information you're saving is neither part of the node's content nor part of the node's metadata. It is metadata about an email that has been sent. Since the email records aren't related to any particular revision of the node, you want ...


6

When displaying information from a config object add it also as cache dependency to the render array. For example in a preprocess hook: $config = \Drupal::config('mymodule.settings'); $config_value = $config->get('some_field'); $variables['some_field'] = [ '#markup' => $config_value, ]; // add the cache tag, so that the output gets ...


6

Normally you would configure the formatted output of an image field in the content type and then render it in the node template: {{ content.field_image }} {{ content|without('field_image') }} If you want to access the path of a private file directly then use the file scheme private://: {{ file_url('private://foo.jpg') }}


6

First, define a library in your theme that links to the font. [THEME].libraries.yml: fonts: css: theme: # Do not include http: or https. # Should start with a double slash. //fonts.googleapis.com/css?family=Source+Sans+Pro: { type: external } This library is loaded by adding it to the theme's [THEME].info.yml file: libraries: - [...


6

You should use the D8 Migrate Modules to do this, specially for transferring 10K users, unless your current code is using the Batch API. Otherwise, you're going to run into timeouts because it's too big. Also, these migrate modules allow you to transfer from SQL to SQL. No need for CSV file, if you don't want. But you can still do CSV file import with them, ...


6

Layouts are defined in the Layout Discovery module and can be used by different layout modules. The difference between those two layouts is the number of regions, the Two Column Brick layout has three more. See the yaml layout definition: /core/modules/layout_discovery/layout_discovery.layouts.yml layout_twocol: label: 'Two column' path: layouts/...


6

Instead of rendering the URL object with toString() use the method Url::getInternalPath: $url = $link->getUrl(); if ($url->isRouted()) { $path = $url->getInternalPath(); }


5

The default Frontpage is a view (admin/structure/views/view/frontpage/edit) showing a waterfall of all content that is Published and "Promoted to the Frontpage". By default, the Frontpage view's Title is "none" while the HTML Title tag is "Home | MySite.com". So you can change the Title of the view there to e.g. "Start"...


5

Caching is shared with multiple users or roles by default, unless you add a cache context like user or user.roles. Otherwise it's hard to tell what the code in the question wants to achieve. If it wants to set block cache keys, then you can't do it from here and you don't need to. They're set automatically. get_agent_number_from_context() should probably ...


5

To get the language specific configuration of a bundle use ContentLanguageSettings::loadByEntityTypeBundle. For the translatability of the bundle, you'd use: $settings = ContentLanguageSettings::loadByEntityTypeBundle($entity_type_id, $bundle_name); $isBundleTranslatable = $settings->getThirdPartySetting("content_translation", "enabled"); You could ...


5

This is the way I do it: // Get actual user role. $current_user_roles = \Drupal::currentUser()->getRoles(); // Check if user is not admin. if (!in_array('administrator', $current_user_roles)) { // Check if user is an admin. if (in_array('administrator', $current_user_roles)) {


5

The answer lies here: $('#portfolioModal', context).once('bootstrap-modal-shown', console.log('*** REGISTER SHOWN.BS.MODAL')) The code that comes after the call to $.once() will only be executed once. However, $.once() can and will be executed multiple times. That's why you are seeing your calls to the console being called multiple times. If you change ...


5

Since Drupal 8.8 modules can now be excluded from configuration synchronization by adding $settings['config_exclude_modules']to your settings.php file. $settings['config_exclude_modules'] = ['devel', 'stage_file_proxy']; See the change record Modules can be excluded from the configuration synchronization.


5

Try adding $form_state->setRebuild(TRUE); to your submitForm(). Quoting from here: Normally, after the entire form processing is completed and submit handlers have run, a form is considered to be done and \Drupal\Core\Form\FormSubmitterInterface::redirectForm() will redirect the user to a new page using a GET request (so a browser refresh does not re-...


5

This is config and you can get it with the following snippet. $admin_theme = \Drupal::config('system.theme')->get('admin'); $admin_theme_name = \Drupal::service('theme_handler')->getName($admin_theme); Or with Drush: drush cget system.theme admin


Only top voted, non community-wiki answers of a minimum length are eligible