diff --git a/field_operations/readme.md b/field_operations/readme.md new file mode 100644 index 0000000..098dd9d --- /dev/null +++ b/field_operations/readme.md @@ -0,0 +1 @@ +Code snippet to access & update field configs for _all_ created fields for a particular entity type, in this case node. diff --git a/field_operations/update-all-fields.php b/field_operations/update-all-fields.php new file mode 100644 index 0000000..dbe2018 --- /dev/null +++ b/field_operations/update-all-fields.php @@ -0,0 +1,34 @@ +getFieldMap(); + $fields = $fields['node']; + $base_fields = $entityFieldManager->getBaseFieldDefinitions('node'); + + // Pop off the entity key & generic fields (nid, uuid, revision_log, etc). + $fields = array_diff_key($fields, $base_fields); + + // getFieldMap returns field names as the array key. + $keys = array_keys($fields); + + // Loop through all fields, plus all bundles that a field is found on. + foreach ($keys as $key => $value) { + $field_name = $keys[$key]; + $bundles = $fields[$value]['bundles']; + + // In this example, we change every custom node field instance to be optional. + // Replace this with whatever specific code you need to change all field instances. + foreach ($bundles as $bundle) { + $config->getEditable("field.field.node.$bundle.$field_name")->set('required', FALSE)->save(); + } + } + +}