0

I have Entity reference field (taxonomy terms ) in node form.

I have checked the option (Create referenced entities if they don't already exist) so that users can create new taxonomy terms in field of node form i.e.: on creation on node new terms creation is allowed.

enter image description here

Looks like below in Node Form:

enter image description here

But I want to alter this and make restricted to certain roles. So that user with particular role should not be able to create new terms and must select from existing node.

My field is core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php

While going thorugh code it seems under :

[#selection_settings] => Array
                        (
                            [target_bundles] => Array
                                (
                                    [topic] => topic
                                )

                            [sort] => Array
                                (
                                    [field] => name
                                    [direction] => asc
                                )

                            [auto_create] => 1
                            [auto_create_bundle] => 
                            [match_operator] => CONTAINS
                        )

[auto_create] => 1 I tried to alter this by adding contain of user role in node form alter, but not its not working the value get changed.

Please suggest !

Update : Please see @ssibal

enter image description here

    drupal config-list field.field.node.link.field_topic2
 field.field.node.link.field_topic2                                                                                                                               
 uuid: f3c818dd-b5cd-4a73-af88-c20c1d0e24c3                                                                                                                       
 langcode: en                                                                                                                                                     
 status: true                                                                                                                                                     
 dependencies:                                                                                                                                                    
   config:                                                                                                                                                        
     - field.storage.node.field_topic2                                                                                                                            
     - node.type.link                                                                                                                                             
     - taxonomy.vocabulary.topic                                                                                                                                  
 id: node.link.field_topic2                                                                                                                                       
 field_name: field_topic2                                                                                                                                         
 entity_type: node                                                                                                                                                
 bundle: link                                                                                                                                                     
 label: Topic                                                                                                                                                     
 description: "Sätt flera olika ämnen, separera dem med kommatecken och inled med stor bokstav.\r\n<br>\r\nExempel: SEB, Fraktionella reserver, Svågerkapitalism" 
 required: true                                                                                                                                                   
 translatable: false                                                                                                                                              
 default_value: {  }                                                                                                                                              
 default_value_callback: ''                                                                                                                                       
 settings:                                                                                                                                                        
   handler: 'default:taxonomy_term'                                                                                                                               
   handler_settings:                                                                                                                                              
     target_bundles:                                                                                                                                              
       topic: topic                                                                                                                                               
     sort:                                                                                                                                                        
       field: name                                                                                                                                                
       direction: asc                                                                                                                                             
     auto_create: true                                                                                                                                            
     auto_create_bundle: ''                                                                                                                                       
 field_type: entity_reference
1

There are multiple ways to achieve this, but I think the most clean is: Config Override.

I suggest to read the whole to understand it, but only this part is needed for you: Providing overrides from modules

You need then only find the configuration you want to override in runtime, and do it based on the current user role.

The field config pattern you are looking for is the following: field.field.entity_type.bundle.field_name

The auto_create is under 'settings/handler_settings'.

HOW TO FIND AND EXAMINE THE CONFIGS YOU NEED

Configs are also entities. They can be feetched with EntityTypeManager. To see what type of entities you have in the system you have the "getDefinitions()" method. Then all you have to do, is to find the right storage (getStorage($config_entity_type)), and then load it by ID. Printing it out with kint, will give you a nice overview how the entity looks like, and how the settings are stored.

You can also "drush cex" your configuration. In this case configuration files will be created. The same scheme can be also found here. It gives also a good overview.

  • Thanks for guidance, I have tested the way you suggested. Similar to example I am able to override site name, please suggest how should I find configuration name. when I used drush config-list it listed : field.storage.node.field_topic2 and field.field.node.link.field_topic2 only but this does not seems to show any value such as auto_create. Please suggest. – jas Mar 21 '18 at 17:08
  • dependencies: config: - field.storage.node.field_topic2 - node.type.link - taxonomy.vocabulary.topic id: node.link.field_topic2 field_name: field_topic2 entity_type: node bundle: link label: Topic required: true translatable: false default_value: { } default_value_callback: '' settings: handler: 'default:taxonomy_term' handler_settings: target_bundles: topic: topic sort: field: name direction: asc auto_create: true auto_create_bundle: '' field_type: entity_reference – jas Mar 21 '18 at 17:24
  • Seems like similar to drupal.stackexchange.com/questions/194796/… – jas Mar 21 '18 at 17:44
  • drush config-get system.site gives values but drush config-get node.link.field_topic2 comes as does not exist in active configuration but similarly drush config-get field.storage.node.field_topic2 gives values. Seems like this is not part of config but some kind of handler settings. Please suggest ! – jas Mar 21 '18 at 17:45
  • I have to create new Field widget and made required changes in formElement of widget based on user role. Hope there is any better solution than this I have to use for now. – jas Mar 22 '18 at 7:09
0

I am able to solve it by 2 methods :

First : http://www.drupalcontrib.org/api/drupal/drupal%21core%21modules%21field%21field.api.php/function/hook_field_widget_WIDGET_TYPE_form_alter/8

function my_module_field_widget_form_alter(&$element, &$form_state, $context) {


if ($element['target_id']['#type'] == 'entity_autocomplete' && $element['target_id']['#target_type'] == 'taxonomy_term' && !empty($element['target_id']['#selection_settings']['target_bundles']['topic'])) {

$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();

    if(!in_array(ROLE_ADMIN, $roles)){
      if (!in_array('my_role', $roles) || in_array('anonymous', $roles)) {
        unset($element['target_id']['#autocreate']);
      }
    }
  }
}

Second

By creating my own widget simialr to Autocomplete tag style and overriding formElement in that to remove auto_create fields support based on user role.

Third:

As suggested by @ssibal [Config Override.][1] (but problem in this )

$current_user = \Drupal::currentUser();
    $roles = $current_user->getRoles();

    if (!in_array('my_role', $roles)) {
        $overrides = $overrides + [
            'field.field.node.link.field_topic2' => [
              'settings' => ['handler_settings' => ['auto_create' => 0 ]],
            ],
          ];
     }


        return $overrides;
  • You can also solve it by this. However this solution is not as robust as the config override one. – ssibal Mar 22 '18 at 9:04
  • I tried but it was not listed in config, please suggest if we can solve it with config overriding , I have mentioned in comments that it was not part of config. – jas Mar 22 '18 at 9:05
  • @ssibal I have update config related to field. The config debug only show few things but not related to auto_create part which I need to override. May be I am missing some part which is not clear to me , please suggest if we can do it with config overrding – jas Mar 22 '18 at 9:08
  • I updated my answer this morning: did you try those? – ssibal Mar 22 '18 at 9:26

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.