Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #2
dreamer777 CreditAttribution: dreamer777 commentedComment #3
anilsharma.online CreditAttribution: anilsharma.online commentedThanks, It's save my time.
Comment #4
imyaro CreditAttribution: imyaro as a volunteer and at DrupalJedi commentedLook how we can do that snippet smaller:
Comment #5
NWOM CreditAttribution: NWOM commentedThanks alot for the code! I hope this functionality gets added to the field widget in the future.
Comment #6
dsaeger CreditAttribution: dsaeger as a volunteer commentedin what file did you add this function?