Drupal StackExchange

How do I programmatically alter the links displayed in the main navigation menu?

1 month 3 weeks ago

The main navigation menu in the site I am currently working on has a lot of menus. I want to hide the disabled ones.

I figured out that it is rendered via the menu_edit_form form.

entity.menu.edit_form: path: '/admin/structure/menu/manage/{menu}' defaults: _entity_form: 'menu.edit' _title_callback: '\Drupal\menu_ui\Controller\MenuController::menuTitle' requirements: _entity_access: 'menu.update'

I could not locate the menu edit form from the above entry. I am thinking of a doing form alter but is there a better way to alter the links shown in the form and also how do we locate the form that is used to display the link.

Binny

Execute form custom validator first before default validation

1 month 3 weeks ago

I have added a custom form validation on node/add page in drupal 8. The purpose of adding this validator is to add a slash before the path alias string. By default if a path alias string is added without a slash then form validator gives an error asking to add a slash before string.

Now the problem is that the default validation is called before the custom validator.

The code have use is like :-

In my form_alter function

if (!is_array($form['#validate'])) { $form['#validate'] = array(); } array_unshift($form['#validate'], 'my_custom_handler');

to add custom validator to the top of validate array.

Although, my custom validator is added to the top of validator array, still default validator is executing first.

Any help will be highly appreciated.

Thank you

Umed Godd

Blocks aren't being assigned to a region

1 month 3 weeks ago

Is there any reason that these blocks wouldn't be assigned to the regions specified? The only way I can get these to appear is to set them manually. Kinda defeats the purpose of defining a region in the first place.

function custom_module_block_info() { $blocks = array(); $blocks['footer_touts'] = array( 'info' => t('Footer Touts'), 'status' => TRUE, 'region' => 'footer', ); $blocks['carrousel'] = array( 'info' => t('Carrousel'), 'status' => TRUE, 'region' => 'hp_content', 'visibility' => BLOCK_VISIBILITY_LISTED, 'pages' => '<front>', ); return $blocks; }
Luis

How do I change the front page?

1 month 3 weeks ago

In commerce guys demos store I toggled off the main menu in commerce kickstart template and have enabled the main menu block to stay on sidebar first block. I want to get a vertical main menu instead of a horizontal one like the demo store default installation.

Besides, I disabled the slide show content. I want to get the products of a specific collection to be displayed in front page. So I went to Site settings > configuration > System information and changed the default front page URL to collection/my-collection.

But the page doesn't display the main menu on sidebar first. The my-collection colletion is displayed but lose that tiny margins that wrap the products.

I follow this drupal.stackexchange post and tried to comment out the lines

function omega_kickstart_alpha_preprocess_page(&$vars) { if ($vars['is_front']) { unset($vars['page']['content']['content']['content']['system_main']['default_message']); } }

in both profiles/omega_kickstart/preprocess/preprocess-page.inc and profiles/commerce_kickstart/preprocess/preprocess-page.inc, but didn't work.

Could somebody please help me?

Caco

Template override doesn't work

1 month 3 weeks ago

I am trying to develop a Drupal 8 module in which I have to override the html.html.twig template. The idea is that I do not want any of the default html.html.twig template returned instead I only want the custom template returned. I have been spending hours Google hunting and trying to make sense of the many examples and I have gotten myself totally confused.

What is happening is the content of my custom template are being displayed in the default html template so I am seeing the custom content inside the default html template rather than overriding the default content.

For the sake of getting this template override working I have simplified the module as much as possible and simply want to return the contents of a variable into a placeholder in the twig template. Once this override is working I can go back and add the code that will create the correct content to be output into a number of variables in the template.

What I do not understand is exactly how to I override the html.html.twig template.

My module is called unsw_blocks and in the unsw_blocks.module I have the following

function unsw_blocks_theme() { $theme = [ 'unsw_blocks' => [ 'variables' => ['test_var' => NULL], ], ]; return $theme; }

In the Unsw_blocksController.php have the following

class Unsw_blocksController extends ControllerBase { public function unsw_blocks($name) { return [ '#theme' => 'unsw_blocks', '#test_var' => $this->t('The variable called test_var simply passes this text itself to the theme template'), ]; } }

In the custom modules templates directory I have unsw-blocks.html.twig which simply contains

<h1>Testing Template</h1> <p>test_var: {{ test_var }}</p>
rickl

Need "last updated" date in RSS feed

1 month 3 weeks ago

I need to output the "Content: Changed" field in an RSS feed so the system can tell whether or not the node has been updated. The Block display shows the date perfectly; the feed outputs nothing. Definitely looks like a bug. any ideas? Thanks in advance! (Drupal 10, BTW.)

Billy Cobin

Change ID of webform hidden element

1 month 3 weeks ago

How can I change the ID of a hidden element (created with the Drupal 6 webform module) using hook_form_alter()?

The ID that it outputs is something like '#edit-submitted-component-name'. I need the ID to be something very custom and I need to avoid changing it with Javascript. The form API does not support the #atrributes token on hidden elements, so I can't set the ID via hook_form_alter();

Any suggestions would be really helpful. Thank You.

Rick Bergmann

Profiles-2--user-edit-page

1 month 3 weeks ago

I made my custom user-profile-edit.tpl.php using

function mytheme_theme($existing, $type, $theme, $path) { $hooks['user_profile_form'] = array( // Forms always take the form argument. 'arguments' => array('form' => NULL), 'render element' => 'form', 'template' => 'templates/user-profile-edit', ); return $hooks; }

Now I want in user-profile-edit.tpl.php to render the account form (default Drupal form for users) and my profiles2 forms, from the forms i created without click the upper links.

Does anyone know how I can achieve this?

Arrok

Convert UTC to local time in date field

1 month 3 weeks ago

I have a content type with a date field, which I convert to a specific date format in a preprocess function:

$variables['date'] = \Drupal::service('date.formatter')->format($variables['node']->field_datum->date->getTimestamp(), 'long_date_without_time');

But as Germany is one hour ahead, a date like 2015-03-31 23:01:08 UTC converts to 1. April 2015. How can I convert this to our local time?

EDIT: I already set the correct timezone in the UI. And I just saw that the wrong dates are only shown when I set my system time between 11pm and 1 am...

0711master