We’re rewarding the question askers & reputations are being recalculated! Read more.

New answers tagged

0

If you want to get the node alias url by languages dont forget to use the language like this. $alias_url = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'. $nid, $langcode); If you are using it in a class do not forget to use the dependencies injection correctly.


6

The created date is stored in the node_field_data table as a UNIX timestamp in a cloumn called created. Here's the code you would use to fetch the nodes that has been created X days ago, using a direct select query. $day = 86400; // One day in seconds. $days = 3; // The amount of days ago to fetch from. $nids = \Drupal::database()->select('...


3

If you go into your Drupal database you should find a table called 'node_field_data'. In this table there is a column labeled 'created'. That will be your Unix timestamp for when the node was created.


0

For somebody looking to find how to do this in Drupal 8 here is the corresponding question and answer How can I get the nid of a node from the page alias? use \Drupal\Core\Url; $alias = \Drupal::service('path.alias_manager')->getPathByAlias('/etapes-de-la-vie'); $params = Url::fromUri("internal:" . $alias)->getRouteParameters(); $entity_type = key($...


0

Provided in case node_type_get_names() gets deprecated in the future (also a nice example of array_map usage): function node_type_get_names() { return array_map(function ($bundle_info) { return $bundle_info['label']; }, \Drupal::service('entity_type.bundle.info') ->getBundleInfo('node')); }


2

After some digging around through the available services and core classes, I have determined you need to get the nid through the path.alias_manager. Additionally you need to wrap everything in a couple of conditions or you will throw errors if the path does not exist. (eg. if you are trying to roll this change out to a fleet of sites that might or might ...


0

You can use the following module. Override Node Options The Override Node Options module allows permissions to be set to each field within the Authoring information and Publishing options field sets on the node form. It also allows selected field sets to be set as collapsed and / or collapsible.


0

If the route for that path is, for example, track-new/%/%node or track-new/%user/%node, I would use the following code. if ($node = menu_get_object('node', 2)) { $content_type = $node->type; } If the route for that path is, for example, track-new/%/% or track-new/%user/%, I would use the following code. $nid = arg(2); if (!empty($nid) && ...


0

If you are asking as administrator of a Drupal site, you don't need to write PHP code to achieve what you described: Whenever it's a block you created from the UI, or it's a block implemented from a module, you can change for which content types the block is shown. On admin/structure/block, find the block you want to show only for some content types. (In ...


0

Try the following , and don't forget to clear the cache function lehigh_preprocess_page(&$variables) { $current_route = \Drupal::routeMatch(); $node = $current_route->getParameter('node'); $target_taxonomy_id = 123; if (isset($node) && $node->getType() == 'standard_content_page' && $node->hasField('...


2

It happens because node_load(), as every Drupal function that loads entities from the database, stores in memory the loaded entity. If you want to avoid that happens, you can use code similar to the following one. for ($i = 0; $i < 1000000; $i++) { $nid = get_node_id($i); $node = entity_load('node', array($nid), array(), TRUE); $data = [ $node-&...


3

To attach a library only when a node in a certain view mode is displayed you can use hook_entity_view/hook_ENTITY_TYPE_view like following. /** * Implements hook_ENTITY_TYPE_view(). */ function MYMODULE_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) { ...


0

Drupal does not distinct between full and default here. If no full viewmode is given for a certain node type than the default viewmode is used. So you could check whether a given node type implements the full viewmode and apply your conditional logic based on this, like this: function hook_page_attachments(array &$attachments) { /* @var \Drupal\node\...


0

I worked out what the issue is, you can only see that bar when you're looking at the Latest Version of a node (using the "Latest Version" tab) not when viewing the latest published version of the node (using the "View" tab). So this is now resolved by checking if they have access to the "Latest Version" tab of a node (basically checking that exists) and ...


0

Ended up resolving this issue with the following code: (function ($, Drupal) { /** * Check/Uncheck all checkboxes. */ Drupal.behaviors.selectAll = { attach: function (context, settings) { $(".form-checkboxes").prepend($('<input type="checkbox" class="form-checkbox shield-select-all"/><label class="option shield-select-all-...


1

You need to get the values first you want to work with. For example the node object is inside $variables['elements']['#node']. In the sample below field_foobar is a text field. Depending on what your field type is you may need to get the value you want a little bit differently. And also maybe sanitize it first before you use it in a template suggestion (...


1

Welcome to Drupal Answers :) You are right, you can do that with the help of a Relationship and a Contextual filter, but details are important. In your Latest articles Views add the Relationship named something like Content referenced from field_myERfield for your featured article field. Be careful about the wording there because there are two ...


0

The save_draft module should make this possible for Drupal 7 (with the patch from issue 1786442 applied). Issue 2220659 describes a Drupal 8 version, but the patch does not include the fix for allowing missing required fields. In Drupal 8, this has become a bit more difficult because of the added Entity validation. I created following ugly solution for my ...


Top 50 recent answers are included