0

For example, How can I add some classes or hide labels and instead of it add placeholder with using of ThemeName_preprocess_comment to 'subject' and 'name' comments form elements?

I have tried to usedrupal_set_message(kint($variables)); in ThemeName_preprocess_comment section, but it cause the empty screen erroe in my site.

So how can I add some classes to default (or my own created) comments elements so I can target them for adding CSS style.

How can I target default comments form element in CSS?

2

i would do it:

1) create new file: testmodule.info.yml.

name: testmodule
description: Testmodule
type: module
core: 8.x

2) create new file: testmodule.module

<?php


function testmodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  if ($form_id=='comment_comment_form') {

    //add class subject
    $form['subject']['widget']['0']['value']['#attributes']['class'][]='newclass_subject';

    //add placeholder subject
    $form['subject']['widget']['0']['value']['#attributes']['placeholder'] = t('Placeholder Subject');


    //add class body
    $form['comment_body']['widget']['0']['#attributes']['class'][]='newclass_body';

    //add placeholder body
    $form['comment_body']['widget']['0']['#attributes']['placeholder'] = t('Placeholder Body');

  }

}
2

if you use Drupal7, you should not use

drupal_set_message(kint($variables));

but

kint($variables) 

or

dpm($variables)

An example that you can see, enter in the following link: https://api.drupal.org/api/drupal/modules%21comment%21comment.module/function/template_preprocess_comment/7.x

function bartik_preprocess_comment(&$variables) {

  $variables['classes_array'][] = 'myclass';
  dpm($variables);

}

You can also create a file "comment-wrapper.tpl.php" and add the classes you want and placeholder.

Example:

<?php

/**
 * @file
 * Bartik's theme implementation to provide an HTML container for comments.
 *
 * Available variables:
 * - $content: The array of content-related elements for the node. Use
 *   render($content) to print them all, or
 *   print a subset such as render($content['comment_form']).
 * - $classes: String of classes that can be used to style contextually through
 *   CSS. It can be manipulated through the variable $classes_array from
 *   preprocess functions. The default value has the following:
 *   - comment-wrapper: The current template type, i.e., "theming hook".
 * - $title_prefix (array): An array containing additional output populated by
 *   modules, intended to be displayed in front of the main title tag that
 *   appears in the template.
 * - $title_suffix (array): An array containing additional output populated by
 *   modules, intended to be displayed after the main title tag that appears in
 *   the template.
 *
 * The following variables are provided for contextual information.
 * - $node: Node object the comments are attached to.
 * The constants below the variables show the possible values and should be
 * used for comparison.
 * - $display_mode
 *   - COMMENT_MODE_FLAT
 *   - COMMENT_MODE_THREADED
 *
 * Other variables:
 * - $classes_array: Array of html class attribute values. It is flattened
 *   into a string within the variable $classes.
 *
 * @see template_preprocess_comment_wrapper()
 */
?>
<?php 
    //add placeholder
    $content['comment_form']['subject']['#attributes']['placeholder'] = t('Title');
    $content['comment_form']['comment_body']['und'][0]['value']['#attributes']['placeholder'] = t('Comment Body');
  ?>
<div id="comments" class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php if ($content['comments'] && $node->type != 'forum'): ?>
    <?php print render($title_prefix); ?>
    <h2 class="title"><?php print t('Comments'); ?></h2>
    <?php print render($title_suffix); ?>
  <?php endif; ?>

  <?php print render($content['comments']); ?>

  <?php if ($content['comment_form']): ?>
    <h2 class="title comment-form "><?php print t('Add new comment'); ?></h2>
    <?php print render($content['comment_form']); ?>
  <?php endif; ?>


</div>
  • Thank you so much for help and reply, I appreciate you greatly, But I use Drupal 8. thanks anyway. – Mojtaba Reyhani Nov 1 '17 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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