0

How do I remove the Author textfield from the Comments form. I added a couple new fields:

  • First Name
  • Last Name

But now in addition to asking for First Name and Last Name, there is a 'Your Name' field which is confusing.

So it looks like this:

  • Add New Comments
  • First Name (text box)
  • Last Name (text box)
  • Your name (text box)
  • Subject (text box)
  • Comment (text box)

I'd like to remove the 'Your name (text box) from all comments since it is a repeating field.

This is the code I am using.

function onviasupport_preprocess_page(&$vars) {
    //modify taxonomy pages breadcrumbs
    if (!empty($vars['template_files'][0]['page-taxonomy'])){
            //pull term id from variable templates list
            foreach($vars['template_files'] as $key => $value){
                if(substr_count($value, 'page-taxonomy-term-') > 0){
                    $tid = explode('-', $value); 
                    $tid = $tid[3]; 
                }
            }
            if (!empty($tid)){ 
                //We're on a taxonomy term page. 
                $term = taxonomy_get_term($tid);
                $link = getLinkByPath();                
                $bc = '<div class="breadcrumb"><a href="/">Home</a>  &#8250; '.$link;
                $vars['breadcrumb'] = $bc. ' &#8250; '. $term->name .'</div>'; 
            }
    }

    //var_dump($vars['page']['content']['system_main']['nodes']); 

    if (!empty($vars['node'])){
        // If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
        $vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
        $vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type.'__'.$vars['node']->nid;

        //modify resource page & article breadcrumbs.. 
        //$lnk = getLinkByType($vars['node']->type);
        $lnk = getLinkByPath();

        $bc = '<div class="breadcrumb"><a href="/">Home</a>';
        if ($lnk) { $bc .= ' &#8250; '.$lnk;}

        //add the first taxonomy term associated with this node
        $tid = _get_node_taxonomy_tid($vars['node']);
        $term = taxonomy_term_load($tid);
        if(!empty($term)) { 
            $bc .= ' &#8250; '.l($term->name, drupal_get_path_alias('taxonomy/term/'.$tid));
        }

        $vars['breadcrumb'] = $bc . ' &#8250; '.$vars['node']->title .'</div>';


        //insert the title emphasis
        //$vars['head_title'] = cleanTitle($vars['head_title']);  
        $vars['title'] = lowerG(insertEmTrig($vars['node']->title));
        //$vars['node']->title = insertEmTrig($vars['head_title']);

        if ($vars['node']->type == 'primary_landing' || $vars['node']->type == 'resource_landing'){ 
            $vars['title'] = lowerG(insertEmTrig($vars['node']->title, '<br />'));
        }
    }
}

//given a node, return the first taxonomy tid assigned
function _get_node_taxonomy_tid($node) {
    $tkeys = array();
    foreach($node as $key => $val) { 
        if(substr_count($key, 'taxonomy')) { 
            $tkeys[] = $key; 
        }
    }
    foreach($tkeys as $tvoc){
        if(!empty($node->$tvoc)) { 
            $tx = $node->$tvoc;
            return $tx['und'][0]['tid'];
        }
    }
}

//create a list of clickable letters to glossary pages. 
function glossary_letters($term = array(), $nodeTx = array()){
    $output = ''; 
    /*
    //if we're on a node, grab the term id
    if(!empty($nodeTx)){
        foreach($nodeTx as $nt){ 
            if($nt->vid == "5"){  //the glossary
                $tid = $nt->tid; 
                break;
            }
        } 
    }
    */

    //make sure this is a term in the glossary vocabulary
    if ($term->vid == "5") {  
        //search through the nodes assigned to this term, and strip off the letter from their title.
        //$sql = taxonomy_select_nodes(array($tid),'or', 0, false, 'n.title DESC');   
        // - not working well due to it's running db_query_range(), fine for a limited range but not this.

        $result = db_query('SELECT DISTINCT(n.nid), n.title, n.created FROM {node} n 
            INNER JOIN taxonomy_index tn ON n.nid = tn.nid 
            WHERE tn.tid = :tid AND n.status = 1', array(':tid' => $term->tid)); 

        $nodes = array();
        foreach ($result as $node){
            $ltr = explode(' - ', $node->title); 
            $ltr = strtoupper($ltr[1]); 
            $nodes[$ltr] = $node->nid;  //letter is now matched to the nid
        }   
        if(!empty($nodes)) {
            $output .= '<div class="clr letters">'; 
            foreach(range('A','Z') as $letter){
                if(array_key_exists($letter, $nodes)){
                    $path = url('node/'.$nodes[$letter]); 
                    $output .= '<a href="'.$path.'">'.$letter.'</a> &nbsp;';
                } else { 
                    $output .= $letter.' &nbsp;';
                }
            }   
            $output .= '</div>';
        }
    }
    return $output;
}


//given a vocabulary object, return the link to the correct primary page
function getVocabLink($voc){
    $link = '';
    if(is_array($voc->nodes)){
        foreach($voc->nodes as $key => $val){ 
            if (empty($link)) { 
                $link = getLinkByType($key); 
            }
        }
    }   
    return $link;
}
function getLinkByPath() { 
    $link = '';
    $pth = drupal_get_path_alias(); 
    if (substr_count($pth, 'product-guide') > 0){ 
        $link = l('Product Guide','product-guide'); 
    }
    if (substr_count($pth, 'resources') > 0){ 
        $link = l('Resources','resources'); 
    }
    if (substr_count($pth, 'your-account') > 0){ 
        $link = l('Your Account','your-account');   
    }
    return $link;
}
function getLinkByType($type){
    if ($type == 'product_guide_page'){ 
        $link = l('Product Guide','product-guide'); 
    }
    if ($type == 'resource_page'){ 
        $link = l('Resources','resources'); 
    }
    if ($type == 'your_account_page'){ 
        $link = l('Your Account','your-account');   
    }
    if ($type == 'user_guide'){ 
        $link = l('Product Guide','user-guides');   
    }
    return $link;
}

function swapTitle($title){ 
    //now change the default Drupal page title, put brand at beginning
    $t = str_replace(' | Onvia', '',$title);  
    return 'ONVIA | '.$t;
}

/* function to force lowercase "g" on gBusiness  */
function lowerG($gtext){ 
    //Don't lowercase the content, as that could be bad for IDs and classes, so I test for all variances.  
    $gtext = str_replace('GBUSINESS','<span class="g">g</span>BUSINESS', $gtext); 
    $gtext = str_replace('GBusiness','<span class="g">g</span>Business', $gtext); 
    $gtext = str_replace('gBusiness','<span class="g">g</span>Business', $gtext); 
    $gtext = str_replace('gBUSINESS','<span class="g">g</span>BUSINESS', $gtext);
    return $gtext; 
}

function onviasupport_breadcrumb($vars){
    if (!empty($vars)) {
        $crumbs = '<div class="breadcrumb">'.implode(' &#8250; ', $vars['breadcrumb']).'</div>';
        $crumbs = str_replace('^','', $crumbs); 
        return $crumbs;
  }
}

/**
*  insert the <em> tag into title using trigger ^
*/
function insertEmTrig($title, $tag=''){
    $title = explode('^',$title);
    if(count($title) > 1) { 
        return $title[0].$tag.' <em>'.$title[1].'</em>'; 
    } else {
        return $title[0];
    }
}

/**
*  clear the trigger ^ for head title
*/
function cleanTitle($title){
    $title = str_replace('^','', $title);
    return $title; 
}

/**
*  insert the <em> tag half-way through the title, auto = no trigger
*/
function insertEmAuto($title, $tag = ''){
        $title = explode(' ',$title);
        $half = (floor(count($title) / 2));
        array_splice($title, $half, 0, $tag.'<em>'); 
        $title = implode(' ',$title). '</em>';
        return $title;
    }

//return a list of node titles as links from array of nodes. Used in articles. 
function get_title_list($rel_nodes){
    if (empty($rel_nodes[0]['nid'])){ 
        return ''; 
    } else {
        $filter = ''; 
        foreach($rel_nodes as $item){
            if (strpos($filter, 'AND') > 0){ 
                $filter .= ' OR nid = '.$item['nid']; 
            } else { 
                $filter .= ' AND nid = '.$item['nid'];
            }
        }
        $query = db_query('SELECT nid, title FROM {node} WHERE status = 1 '.$filter.' ORDER BY created DESC');
        $list = ''; 
        while ($result = db_fetch_object($query)){
            $itemtitle =  cleanTitle($result->title); 
            $list .= '<dd>'.l($itemtitle, 'node/'.$result->nid).'</dd>';
        }
        return $list; 
    }
}


/**
* This is a theme override of the theme_table function from Drupal's  theme.inc file.  
* Only thing I'm doing here is stripping out any ^ symbols
*/
function onviasupport_table($variables) {
  $header = $variables['header'];
  $rows = $variables['rows'];
  $attributes = $variables['attributes'];
  $caption = $variables['caption'];
  $colgroups = $variables['colgroups'];
  $sticky = $variables['sticky'];
  $empty = $variables['empty'];

  // Add sticky headers, if applicable.
  if (count($header) && $sticky) {
    drupal_add_js('misc/tableheader.js');
    // Add 'sticky-enabled' class to the table to identify it for JS.
    // This is needed to target tables constructed by this function.
    $attributes['class'][] = 'sticky-enabled';
  }

  $output = '<table' . drupal_attributes($attributes) . ">\n";

  if (isset($caption)) {
    $output .= '<caption>' . $caption . "</caption>\n";
  }

  // Format the table columns:
  if (count($colgroups)) {
    foreach ($colgroups as $number => $colgroup) {
      $attributes = array();

      // Check if we're dealing with a simple or complex column
      if (isset($colgroup['data'])) {
        foreach ($colgroup as $key => $value) {
          if ($key == 'data') {
            $cols = $value;
          }
          else {
            $attributes[$key] = $value;
          }
        }
      }
      else {
        $cols = $colgroup;
      }

      // Build colgroup
      if (is_array($cols) && count($cols)) {
        $output .= ' <colgroup' . drupal_attributes($attributes) . '>';
        $i = 0;
        foreach ($cols as $col) {
          $output .= ' <col' . drupal_attributes($col) . ' />';
        }
        $output .= " </colgroup>\n";
      }
      else {
        $output .= ' <colgroup' . drupal_attributes($attributes) . " />\n";
      }
    }
  }

  // Add the 'empty' row message if available.
  if (!count($rows) && $empty) {
    $header_count = 0;
    foreach ($header as $header_cell) {
      if (is_array($header_cell)) {
        $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
      }
      else {
        $header_count++;
      }
    }
    $rows[] = array(array('data' => $empty, 'colspan' => $header_count, 'class' => array('empty', 'message')));
  }

  // Format the table header:
  if (count($header)) {
    $ts = tablesort_init($header);
    // HTML requires that the thead tag has tr tags in it followed by tbody
    // tags. Using ternary operator to check and see if we have any rows.
    $output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
    foreach ($header as $cell) {
      $cell = tablesort_header($cell, $header, $ts);
      $output .= _theme_table_cell($cell, TRUE);
    }
    // Using ternary operator to close the tags based on whether or not there are rows
    $output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
  }
  else {
    $ts = array();
  }

  // Format the table rows:
  if (count($rows)) {
    $output .= "<tbody>\n";
    $flip = array('even' => 'odd', 'odd' => 'even');
    $class = 'even';
    foreach ($rows as $number => $row) {
      // Check if we're dealing with a simple or complex row
      if (isset($row['data'])) {
        $cells = $row['data'];
        $no_striping = isset($row['no_striping']) ? $row['no_striping'] : FALSE;

        // Set the attributes array and exclude 'data' and 'no_striping'.
        $attributes = $row;
        unset($attributes['data']);
        unset($attributes['no_striping']);
      }
      else {
        $cells = $row;
        $attributes = array();
        $no_striping = FALSE;
      }
      if (count($cells)) {
        // Add odd/even class
        if (!$no_striping) {
          $class = $flip[$class];
          $attributes['class'][] = $class;
        }

        // Build row
        $output .= ' <tr' . drupal_attributes($attributes) . '>';
        $i = 0;
        foreach ($cells as $cell) {
          $cell = tablesort_cell($cell, $header, $ts, $i++);
          $output .= _theme_table_cell(cleanTitle($cell));
        }
        $output .= " </tr>\n";
      }
    }
    $output .= "</tbody>\n";
  }

  $output .= "</table>\n";
  return $output;
}

closed as too broad by kiamlaluno May 30 '15 at 5:16

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • In the shown code I don't see any part that is relevant for the question, nor do I see any code trying to remove the field as the question asks. Since the question is about code to write, we expect to see a tentative to do as reported in the question. In that case, we can point out what is wrong. Differently, the question is too broad. We are not here to tell you the best way of doing something, but to answer a well defined question with a definitive answer. The answer could also change over the time, but at least there is one. – kiamlaluno May 30 '15 at 5:22
1

You could do use a hook_form_alter(), eg, something like this:

function YOURMODULE_form_comment_form_alter(&$form, &$form_state) {
  global $user;

  if (!$user->uid) {
    unset($form['author']['name']);
  }

}

to remove that field for anonymous users. If you want that field gone for everyone, then just remove the if :)

Note: you could also unset() the whole $form['author'] but then you would not be able to allow other contact information, eg, the homepage and email address, when you use:

enter image description here

0

You have several options. One is to simply hide the field by using CSS. As the author field is not required it should work like this without problems.

The other is to do some form altering. You can do this in template.php on the theme level or in a custom module.

There is a pretty good description here: http://planken.org/2013/10/drupal-remove-your-name-elements-comment-form

It boils down to this in template.php:

function YOURTHEME_form_comment_form_alter(&$form, &$form_state) {
  $form['author']['#access'] = FALSE;
}

or to this in a custom module:

function YOURMODULE_form_comment_form_alter(&$form, &$form_state) {
  $form['author']['#access'] = FALSE;
}
  • How would I hide the field using CSS? – user3075987 Feb 21 '14 at 0:53
  • @user3075987 You open the site in a browser, open the developer tools available to you to inspect the element, look for the wrapper div of the form element and use it's ID or class to selectively set display: none; on that element. This would have to be done in one of the CSS files of your theme. The proper solution is the form altering though ;) – berliner Feb 21 '14 at 0:56
  • 1
    It would be along the lines of #comment-form .form-item-name { display: none; } but the alter is the right way to go. – Jimajamma Feb 21 '14 at 1:01
  • I put this into the template.php file and it broke my site: <?php function MYTHEME_form_comment_form_alter(&$form, &$form_state) { $form['author']['#access'] = FALSE; } ?> – user3075987 Feb 21 '14 at 1:21
  • @user3075987 How did it break your site? – berliner Feb 21 '14 at 8:43

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