2

Suppose i have a module1 where i define fields and i get list of all the fields in module2 using module1_fields_info(); this is done in D7. How to get list of fields defined by module1 in module2 in Drupal 8?

3

hook_field_info() defines field types in Drupal 7, so I think you're actaully interested in field types, not fields?

If so, field types, like any other plugin definition have a provider key, if you want to get the field types of a certain modules, call \Drupal::service('plugin.manager.field.field_type')->getDefinitions() and then filter on the provider key:

$field_types = \Drupal::service('plugin.manager.field.field_type')->getDefinitions();
$field_types = array_filter($field_types, function (array $definition) { 
  return $definition['provider'] == 'yourmodule';
});

Code example written in browser and untested.

| improve this answer | |
2

This depends on how module1 defines the fields. Supposed it defines an entity type with the module name you can get the field informations like in this example for the user module:

$field_manager = \Drupal::service('entity_field.manager');
$fields = $field_manager->getFieldDefinitions('user', 'user');

The first parameter is the entity type and the second the bundle. If an entity has no bundles you use the entity type a second time.

If module1 defines a content type, then you get the fields by using the same function on the node and the content type name:

$fields = $field_manager->getFieldDefinitions('node', 'module1_type');

If module1 defines extra fields on another entity, for example the user, you get all fields defined in hook_entity_extra_field_info() hooks by module1 and other modules:

$extra_fields = $field_manager->getExtraFields('user', 'user');

The result in all cases is a keyed array with field definitions. You get the field names with

$field_names = array_keys($fields);
| improve this answer | |
  • Good answer for the question title, but I think it is actually about field types due to the hook_field_info() reference, see my answer. – Berdir Feb 5 '17 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.