Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field config update code snippet for common settings #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions field_operations/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Code snippet to access & update field configs for _all_ created fields for a particular entity type, in this case node.
34 changes: 34 additions & 0 deletions field_operations/update-all-fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Sets all non-title fields to optional.
*/
function hook_update_N() {
// Services we need.
$entityFieldManager = \Drupal::service('entity_field.manager');
$config = \Drupal::service('config.factory');

// Get all node fields.
$fields = $entityFieldManager->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);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "18" a magic number? How would you do this more generically?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

18 is a magic number, because when I var_dump($fields), the first 18 results for node fields are:

[0]=>
  string(3) "nid"
  [1]=>
  string(4) "uuid"
  [2]=>
  string(3) "vid"
  [3]=>
  string(8) "langcode"
  [4]=>
  string(4) "type"
  [5]=>
  string(5) "title"
  [6]=>
  string(3) "uid"
  [7]=>
  string(6) "status"
  [8]=>
  string(7) "created"
  [9]=>
  string(7) "changed"
  [10]=>
  string(7) "promote"
  [11]=>
  string(6) "sticky"
  [12]=>
  string(18) "revision_timestamp"
  [13]=>
  string(12) "revision_uid"
  [14]=>
  string(12) "revision_log"
  [15]=>
  string(29) "revision_translation_affected"
  [16]=>
  string(16) "default_langcode"
  [17]=>
  string(4) "path"

I think there is a isBaseField() method somewhere that would probably be better to check against, but I was having trouble calling it in this case.

// 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) {
Copy link
Member

@mike-potter mike-potter Nov 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need a comment here to say something like "In this example, we will set each field instance to be optional. Replace this with whatever specific code you need to change the field instances."

$config->getEditable("field.field.node.$bundle.$field_name")->set('required', FALSE)->save();
}
}

}