0

I am facing an issue where hook_form_FORM_ID_alter() code is working in bartik theme while same is not working in Bootstrap theme. Any idea what could be the reason or am I missing something? Very Thanks.

In THEMENAME.theme of bootstrap theme

<?php
/**
 * @file
 * Bootstrap sub-theme.
 *
 * Place your custom PHP code in this file.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;


function subtheme_form_search_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['keys']['#attributes']['placeholder'] = t('Sentence for placeholder!!!.');  
}

OtherWay,

function subtheme_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if($form_id == 'search_block_form'){
      $form['keys']['#attributes']['placeholder'] = t('Sentence for placeholder!!!.');
    }
}

In bartik.theme

<?php

/**
 * @file
 * Functions to support theming in the Bartik theme.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;

function bartik_form_search_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['keys']['#attributes']['placeholder'] = t('Sentence for placeholder!!!.');  
}
5
  • You have two placeholders for the theme name. Are they the same THEMENAME == subtheme?
    – 4uk4
    Dec 21, 2016 at 7:30
  • @4k4 Hi, yes it is my actual sub theme name.
    – CodeNext
    Dec 21, 2016 at 7:32
  • I would try a less specific THEMENAME_form_alter() and debug the form_id's that are coming in.
    – 4uk4
    Dec 21, 2016 at 7:37
  • @4k4 I will try that way and will let you know.
    – CodeNext
    Dec 21, 2016 at 9:05
  • @4k4 I have tried that way also, but no luck, updated the question with the code which I have used. Any suggestions, thanks.
    – CodeNext
    Dec 23, 2016 at 5:04

1 Answer 1

0

You can try a bootstrap plugin:

themes/THEMENAME/src/Plugin/Form/SearchBlockForm.php

/**
 * @file
 * Contains \Drupal\THEMENAME\Plugin\Form\SearchBlockForm.
 */

namespace Drupal\THEMENAME\Plugin\Form;

use Drupal\bootstrap\Annotation\BootstrapForm;
use Drupal\bootstrap\Utility\Element;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @ingroup plugins_form
 *
 * @BootstrapForm("search_block_form")
 */
class SearchBlockForm extends \Drupal\bootstrap\Plugin\Form\SearchBlockForm {

  /**
   * {@inheritdoc}
   */
  public function alterForm(array &$form, FormStateInterface $form_state, $form_id = NULL) {
    // Call the parent method from the base theme, if applicable (which it is
    // in this case because Bootstrap actually implements this alter).
    parent::alterForm($form, $form_state, $form_id);

    $form['keys']['#attributes']['placeholder'] = t('Sentence for placeholder!!!.')
  }
}

Reference:

http://drupal-bootstrap.org/api/bootstrap/docs%21plugins%21Form.md/group/plugins_form/8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.