Regarding the CVS message of Fieldgroup module (now part of 4.7.x-dev), September 5, 2006:

now a fieldgroup is hidden, if all its fields are hidden.

Shall we understand that is actually possible to hide CCK fields? If yes, how?

thanks

Comments

KarenS’s picture

Category: support » feature

At the moment, this can only be done with a custom module using hook_form_alter(). Perhaps someone will propose another way (a new widget or an extension of some current widget).

So I guess I will make this a feature request since I don't think it is already proposed.

marcoBauli’s picture

Thanks

also an alternative less techie way to do this is using the form_markup.module to assign a CSS <div id="hidden"> class to the fields to hide, and hide them in the style.css (display:none;).

fago’s picture

you are right, currently hook_form_alter() has to be used to hide fields.

marcoBauli’s picture

uhm.. could anyone be so kind to post an example of this using hook_form_alter() for most of us mortals? :)

chelahmy’s picture

I've made a patch for 5.x-1.2 to hide any field. I'm not sure whether it will apply to the earlier versions. With this feature I don't need to theme a node just to hide the default field listing when I use dript.

The hidden attribute is similar to the required attribute. A new column 'hidden' is added in the node_field table so update.php need to be applied. I do not remove any hidden field from cck processing. However in the function theme_field() I make it return an empty string when a field is flagged as hidden.

Similar to the required field, I notice that all nodes type that share a field will also share the required and hidden attributes. There is no way two nodes with an identical field will be allowed to set the required and hidden attributes of the individual fields separately.

Field will not be made hidden automatically in custom theme. For theme writer, always check the field's hidden attribute. We can still manipulate hidden fields in the background, or we can force to display hidden fields anyway.

The patch involves three files: content.install, content.module, and content_admin.inc. I tried to attach the diff files here but it could take only one file. It doesn't accept zip.

Patch in content.install:

A column 'hidden' need to be added in table node_field. I added an update instruction with sequence number 1002 (its the next number in my copy).

/**
 * Add hidden flag.
 */
function content_update_1002() {
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_add_column($ret, 'node_field', 'hidden', 'integer', array('not null' => TRUE, 'default' => '0'));
      break;

    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node_field} ADD COLUMN hidden int NOT NULL default 0");
      break;
  }

  return $ret;
}

Patch in content.module:

Force theme_field() function not to process hidden field.

Before

function theme_field(&$node, &$field, &$items, $teaser, $page) {
  $items_output = '';

  ...

After

function theme_field(&$node, &$field, &$items, $teaser, $page) {
  if ($field['hidden'] != 0)
    return '';

  $items_output = '';

  ...

Patch in content_admin.inc #1:

Add the 'hidden' check box in field administration form.

Before

function _content_admin_field($type_name, $field_name) {
  ...

  $form['field']['multiple'] = array(
    '#type' => 'checkbox',
    '#title' => t('Multiple values'),
    '#default_value' => $field['multiple'],
  );

  $additions = module_invoke($field_type['module'], 'field_settings', 'form', $field);
  if (is_array($additions)) {
    $form['field'] = array_merge($form['field'], $additions);
  }

  ...

After

function _content_admin_field($type_name, $field_name) {
  ...

  $form['field']['multiple'] = array(
    '#type' => 'checkbox',
    '#title' => t('Multiple values'),
    '#default_value' => $field['multiple'],
  );

  $form['field']['hidden'] = array(
    '#type' => 'checkbox',
    '#title' => t('Hidden'),
    '#default_value' => $field['hidden'],
  );

  $additions = module_invoke($field_type['module'], 'field_settings', 'form', $field);
  if (is_array($additions)) {
    $form['field'] = array_merge($form['field'], $additions);
  }

  ...

Patch in content_admin.inc #2:

Update a field hidden status in the database.

Before

function _content_admin_field_submit($form_id, $form_values) {
  ...

  db_query("UPDATE {node_field} SET required = %d, multiple = %d, global_settings = '%s', db_storage = %d WHERE field_name = '%s'", $form_values['required'], $form_values['multiple'], serialize($field_settings), $field['db_storage'], $form_values['field_name']);

  ...

After

function _content_admin_field_submit($form_id, $form_values) {
  ...

  db_query("UPDATE {node_field} SET required = %d, multiple = %d, hidden = %d, global_settings = '%s', db_storage = %d WHERE field_name = '%s'", $form_values['required'], $form_values['multiple'], $form_values['hidden'], serialize($field_settings), $field['db_storage'], $form_values['field_name']);

  ...
yched’s picture

chelah : this thread is about hiding fields and fieldsets in the edit / creation form, whereas your patch deals with field display.
Besides latest cck 1.3 release now has an advanced field display page, that allows you to choose the displayed fields on teaser / full page view.

chelahmy’s picture

It's great if cck 1.3 have the feature I'm looking for. I'd been searching cck feature request list and found this thread with the same title of my problem. I've been following cck and had no idea when 1.3 will realease and will it include hiding a field. So I just went through the code and found an answer to my problem. Well, I'm thinking to contribute it back. Anyway, I'll be glad if anybody can move the post to a proper thread.

edrex’s picture

Status: Active » Closed (fixed)

cck 1.3 has field hiding