Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now
3 added 4 characters in body
source | link

D7. Here we had to make sure that when adding a node one certain taxonomy term reference option is always uncheckable and will always be saved to the node. So we went into #after_build and disabled that certain option, but had to make sure that in the end that certain option will be passed on. Cause only disabling it would have stopped that option's travel to future hooks.

// a constant
define('MYTERM', 113);

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'MYCONTENTTYPE_node_form') {
    $form['#after_build'][] = 'MYMODULE_MYCONTENTTYPE_node_form_after_build';
    }
}

/**
 * Implements custom after_build_function()
 */
function MYMODULE_MYCONTENTTYPE_node_form_after_build($form, &$form_state) {
  foreach (element_children($form['field_MYFIELD'][LANGUAGE_NONE]) as $tid) {
    if ($tid == MYTERM) {
      $element = &$form['field_MYFIELD'][LANGUAGE_NONE][$tid];
      $element['#checked'] = TRUE;
      $element['#attributes']['disabled'] = 'disabled';
    }
  }
  // here's ensured the term's travel goes on
  $form['field_MYFIELD'][LANGUAGE_NONE]['#value'] += drupal_map_assoc(array(MYTERM));
  return $form;
}

That's how the disabled option looks like:

enter image description here

Here we had to make sure that when adding a node one certain taxonomy term reference option is always uncheckable and will always be saved to the node. So we went into #after_build and disabled that certain option, but had to make sure that in the end that certain option will be passed on. Cause only disabling it would have stopped that option's travel to future hooks.

// a constant
define('MYTERM', 113);

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'MYCONTENTTYPE_node_form') {
    $form['#after_build'][] = 'MYMODULE_MYCONTENTTYPE_node_form_after_build';
    }
}

/**
 * Implements custom after_build_function()
 */
function MYMODULE_MYCONTENTTYPE_node_form_after_build($form, &$form_state) {
  foreach (element_children($form['field_MYFIELD'][LANGUAGE_NONE]) as $tid) {
    if ($tid == MYTERM) {
      $element = &$form['field_MYFIELD'][LANGUAGE_NONE][$tid];
      $element['#checked'] = TRUE;
      $element['#attributes']['disabled'] = 'disabled';
    }
  }
  // here's ensured the term's travel goes on
  $form['field_MYFIELD'][LANGUAGE_NONE]['#value'] += drupal_map_assoc(array(MYTERM));
  return $form;
}

That's how the disabled option looks like:

enter image description here

D7. Here we had to make sure that when adding a node one certain taxonomy term reference option is always uncheckable and will always be saved to the node. So we went into #after_build and disabled that certain option, but had to make sure that in the end that certain option will be passed on. Cause only disabling it would have stopped that option's travel to future hooks.

// a constant
define('MYTERM', 113);

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'MYCONTENTTYPE_node_form') {
    $form['#after_build'][] = 'MYMODULE_MYCONTENTTYPE_node_form_after_build';
    }
}

/**
 * Implements custom after_build_function()
 */
function MYMODULE_MYCONTENTTYPE_node_form_after_build($form, &$form_state) {
  foreach (element_children($form['field_MYFIELD'][LANGUAGE_NONE]) as $tid) {
    if ($tid == MYTERM) {
      $element = &$form['field_MYFIELD'][LANGUAGE_NONE][$tid];
      $element['#checked'] = TRUE;
      $element['#attributes']['disabled'] = 'disabled';
    }
  }
  // here's ensured the term's travel goes on
  $form['field_MYFIELD'][LANGUAGE_NONE]['#value'] += drupal_map_assoc(array(MYTERM));
  return $form;
}

That's how the disabled option looks like:

enter image description here

2 deleted 61 characters in body
source | link

Here we had to make sure that when adding a node one certain taxonomy term reference option is always uncheckable and will always be saved to the node. So we went into #after_build and disabled that certain option, but had to make sure that in the end that certain option will be passed on. Cause only disabling it would have stopped that option's travel to future hooks.

// a constant
define('DOC_KAT_PRESSEMATERIAL_TID''MYTERM', 113);

/**
 * Implements hook_form_alter().
 */
function doc_kat_form_alterMYMODULE_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'pressematerial_node_form''MYCONTENTTYPE_node_form') {
    $form['#after_build'][] = 'doc_kat_pressematerial_node_form_after_build';'MYMODULE_MYCONTENTTYPE_node_form_after_build';
    }
}

/**
 * Implements custom after_build_function()
 */
function doc_kat_pressematerial_node_form_after_buildMYMODULE_MYCONTENTTYPE_node_form_after_build($form, &$form_state) {
  foreach (element_children($form['field_dokumentenart'][LANGUAGE_NONE]$form['field_MYFIELD'][LANGUAGE_NONE]) as $tid) {
    if ($tid == DOC_KAT_PRESSEMATERIAL_TIDMYTERM) {
      $element = &$form['field_dokumentenart'][LANGUAGE_NONE][$tid];&$form['field_MYFIELD'][LANGUAGE_NONE][$tid];
      $element['#checked'] = TRUE;
      $element['#attributes']['disabled'] = 'disabled';
    }
  }
  // here's ensured the term's travel goes on
  $form['field_dokumentenart'][LANGUAGE_NONE]['#value']$form['field_MYFIELD'][LANGUAGE_NONE]['#value'] += drupal_map_assoc(array(DOC_KAT_PRESSEMATERIAL_TIDMYTERM));
  return $form;
}

That's how the disabled option looks like:

enter image description here

Here we had to make sure that when adding a node one certain taxonomy term reference option is always uncheckable and will always saved to the node. So we went into #after_build and disabled that certain option, but had to make sure that in the end that certain option will be passed on. Cause only disabling it would have stopped that option's travel to future hooks.

define('DOC_KAT_PRESSEMATERIAL_TID', 113);

/**
 * Implements hook_form_alter().
 */
function doc_kat_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'pressematerial_node_form') {
    $form['#after_build'][] = 'doc_kat_pressematerial_node_form_after_build';
    }
}

/**
 * Implements custom after_build_function()
 */
function doc_kat_pressematerial_node_form_after_build($form, &$form_state) {
  foreach (element_children($form['field_dokumentenart'][LANGUAGE_NONE]) as $tid) {
    if ($tid == DOC_KAT_PRESSEMATERIAL_TID) {
      $element = &$form['field_dokumentenart'][LANGUAGE_NONE][$tid];
      $element['#checked'] = TRUE;
      $element['#attributes']['disabled'] = 'disabled';
    }
  }
  // here's ensured the travel goes on
  $form['field_dokumentenart'][LANGUAGE_NONE]['#value'] += drupal_map_assoc(array(DOC_KAT_PRESSEMATERIAL_TID));
  return $form;
}

That's how the disabled option looks like:

enter image description here

Here we had to make sure that when adding a node one certain taxonomy term reference option is always uncheckable and will always be saved to the node. So we went into #after_build and disabled that certain option, but had to make sure that in the end that certain option will be passed on. Cause only disabling it would have stopped that option's travel to future hooks.

// a constant
define('MYTERM', 113);

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'MYCONTENTTYPE_node_form') {
    $form['#after_build'][] = 'MYMODULE_MYCONTENTTYPE_node_form_after_build';
    }
}

/**
 * Implements custom after_build_function()
 */
function MYMODULE_MYCONTENTTYPE_node_form_after_build($form, &$form_state) {
  foreach (element_children($form['field_MYFIELD'][LANGUAGE_NONE]) as $tid) {
    if ($tid == MYTERM) {
      $element = &$form['field_MYFIELD'][LANGUAGE_NONE][$tid];
      $element['#checked'] = TRUE;
      $element['#attributes']['disabled'] = 'disabled';
    }
  }
  // here's ensured the term's travel goes on
  $form['field_MYFIELD'][LANGUAGE_NONE]['#value'] += drupal_map_assoc(array(MYTERM));
  return $form;
}

That's how the disabled option looks like:

enter image description here

1
source | link

Here we had to make sure that when adding a node one certain taxonomy term reference option is always uncheckable and will always saved to the node. So we went into #after_build and disabled that certain option, but had to make sure that in the end that certain option will be passed on. Cause only disabling it would have stopped that option's travel to future hooks.

define('DOC_KAT_PRESSEMATERIAL_TID', 113);

/**
 * Implements hook_form_alter().
 */
function doc_kat_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'pressematerial_node_form') {
    $form['#after_build'][] = 'doc_kat_pressematerial_node_form_after_build';
    }
}

/**
 * Implements custom after_build_function()
 */
function doc_kat_pressematerial_node_form_after_build($form, &$form_state) {
  foreach (element_children($form['field_dokumentenart'][LANGUAGE_NONE]) as $tid) {
    if ($tid == DOC_KAT_PRESSEMATERIAL_TID) {
      $element = &$form['field_dokumentenart'][LANGUAGE_NONE][$tid];
      $element['#checked'] = TRUE;
      $element['#attributes']['disabled'] = 'disabled';
    }
  }
  // here's ensured the travel goes on
  $form['field_dokumentenart'][LANGUAGE_NONE]['#value'] += drupal_map_assoc(array(DOC_KAT_PRESSEMATERIAL_TID));
  return $form;
}

That's how the disabled option looks like:

enter image description here