Drupal StackExchange

How do I conditionally add a field?

1 month 3 weeks ago

I'm trying to find out any example of adding new conditional field using ajax in Drupal 8 form API, but I can't find it.

This is my code.

$form['name'] = array( '#type' => 'textfield', '#title' => $this->t('Name'), '#maxlength' => 64, '#size' => 64, '#default_value' => $name, ); $form['floor'] = array( '#type' => 'textfield', '#title' => $this->t('Floor'), '#default_value' => $floor, ); $form['flat_per_floor'] = array( '#type' => 'textfield', '#title' => $this->t('Flat Per Floor'), '#default_value' => $flatPerFloor, '#ajax' => array( 'callback' => 'Drupal\bms_flat\Form\FlatForm::flatGenerate', 'effect' => 'fade', 'event' => 'change', 'method' => 'replace', 'progress' => array( 'type' => 'throbber', 'message' => "Generating..", ), ), ); if ($form_values['floor']) { $form['flats'] = array( '#type' => 'checkboxes', '#title' => $this->t('Content Type'), '#options' => node_type_get_names(), ); } $form['submit'] = array( '#type' => 'submit', '#title' => $this->t('Submit'), '#default_value' => $this->t($submit), );

This is the callback function.

public function flatGenerate(array &$form, FormStateInterface $form_state) { // … }

How do I add a new field after filling-up my Flat Per Floor field?

Imran Sarder

Multiple blogs per user, each with a blog profile page

1 month 3 weeks ago

I'm trying to to create a site where users upon admin approval, can have multiple blogs, typically differentiated by language.

Each blog should have a profile/landing page specific to that blog.

I can simply create a taxonomy term where a given term is a given blog name and I can use the Taxonomy Permissions module to restrict a given term to a given user.

The part that I'm having trouble thinking through is the admin side setup so that someone who doesn't know a lot about Drupal would be able to simply set up a new blog.

ergophobe

Main navigation translation reverts to default or doesn't display translations

1 month 3 weeks ago

I administer a multilingual site (en - fr - nl) I have translated the menu items in the main navigation but the translated menu items only come up sometimes from a page and never on the front page although the language is chosen from the dropdown language selector and the content correctly changes to the selected language. How can I force the menu to change to the required language when selected and then keep the selected language for all content and menus?

I'll try to explain what happens

  • I arrive on the front page (default language)
  • I select the language
  • Front page content changes to selected language
  • Main navigation does not change language
  • I click on an item in selected language
  • I go to the correct language version of the page
  • The main navigation menu sometimes changes to the translated menu
  • Home always sends to default language home and not translation

Any help gratefully received, thanks

The website is here: https://bso-orchestra.be

Chris Newbie

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