I had a goal to disable "Remove" and "Add more" button for a specific Filed collection with unlimited values for an exact User role.

Maybe someone will need this code

<?php

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == '$FORM_ID') {
  if (user_has_role($RID)) {
    $fdps = $form[$FIELD_COLLECTION_NAME]['und'];
    $i = 0;
    foreach ($fdps as $item) {
	if($fdps[$i]) {
	 unset($form[$FIELD_COLLECTION_NAME]['und'][$i]['remove_button']); // Disable Remove button
	 $i++;
	 }
    }
	unset($form[$FIELD_COLLECTION_NAME]['und']['add_more']); // Disable Add more button
   
  }
  }
}

?>

Is there an ability to quicly search for all buttons if I have several field collections in one node?

Comments

dreamer777 created an issue. See original summary.

dreamer777’s picture

Issue summary: View changes
anilsharma.online’s picture

Thanks, It's save my time.

imyaro’s picture

Look how we can do that snippet smaller:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_FORM_ID_alter(&$form, &$form_state) {
  if (user_has_role($RID)) {
    foreach ($form['FIELD_COLLECTION_NAME'][LANGUAGE_NONE] as &$item) {
      $item['remove_button']['#access'] = FALSE; // Disable Remove button
    }
    $form['FIELD_COLLECTION_NAME'][LANGUAGE_NONE]['add_more']['#access'] = FALSE; // Disable Add more button
  }
}
NWOM’s picture

Thanks alot for the code! I hope this functionality gets added to the field widget in the future.

dsaeger’s picture

in what file did you add this function?