Province/State and Country labels require excessive permissions

Created on 4 September 2025, about 1 month ago

Problem/Motivation

I created a view that lists contacts and their countries/states for users that do not have "view all contacts" permissions.

As a result, they can't see their contacts countries/states (they get a blank).

Proposed resolution

The source file src/SupportedEntities.php needs a tiny fix.

The api permission for listing countries and states only required "access civicrm" and while even this seems excessive, we might as well be consistent ...

Remaining tasks

A minor PR forthcoming.

User interface changes

None.

πŸ› Bug report
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada adixon

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @adixon
  • πŸ‡ΊπŸ‡ΈUnited States markusa

    One way to get these in Views is to make a Relationship and then add the state/country name field via the Relationship.

    We've often done a small custom

    
    /**
     * Implements hook_civicrm_alter_drupal_entities().
     */
    function civicrm_tweaks_civicrm_alter_drupal_entities(&$civicrm_entity_info) {
      $civicrm_entity_info['civicrm_country']['permissions']['view'] = ['access content'];
      $civicrm_entity_info['civicrm_state_province']['permissions']['view'] = ['access content'];
    }
    
    

    Good argument with the permissions matching the API permissions .. makes sense.

  • πŸ‡¨πŸ‡¦Canada adixon

    Thanks for the custom code hint, that would allow even anonymous access, which seems reasonable for most installs.

    I'm unclear about your initial sentence, do you mean if I add a relationship to the country entity from the address (for example), then I'd get access to the list of countries without worrying about the permissions problem?

  • πŸ‡ΊπŸ‡ΈUnited States markusa

    Yes the permission problem can be avoided with a different View construction.
    If you have a base table of Addresses, (or contacts with a relationship to Address)
    and you add the "State/Province ID" field to the View, what this is adding the field handler for an Entity Reference field, and likely you're outputing the entity label, which is the State/Province name.
    Outputting the label counts as "Rendering the entity" .. and anytime you render the entity then access control rules apply.
    You'll find that if you output only the ID that you won't have the problem with the value not appearing.

    So it is possible to add a Views Relationship "CiviCRM State/Province" to the View.

    Now you will be able to add the "State Name" field which uses that relationship you added.

    Since this is just outputting the field value, and not "Rendering the entity" then the access control rule doesn't apply.

    Example:

    langcode: en
    status: true
    dependencies:
      module:
        - civicrm_entity
    id: address_demo
    label: 'Address Demo'
    module: views
    description: ''
    tag: ''
    base_table: civicrm_address
    base_field: id
    display:
      default:
        id: default
        display_title: Default
        display_plugin: default
        position: 0
        display_options:
          fields:
            name_1:
              id: name_1
              table: civicrm_state_province
              field: name
              relationship: state_province_id
              group_type: group
              admin_label: ''
              entity_type: civicrm_state_province
              entity_field: name
              plugin_id: field
              label: ''
              exclude: false
              alter:
                alter_text: false
                text: ''
                make_link: false
                path: ''
                absolute: false
                external: false
                replace_spaces: false
                path_case: none
                trim_whitespace: false
                alt: ''
                rel: ''
                link_class: ''
                prefix: ''
                suffix: ''
                target: ''
                nl2br: false
                max_length: 0
                word_boundary: true
                ellipsis: true
                more_link: false
                more_link_text: ''
                more_link_path: ''
                strip_tags: false
                trim: false
                preserve_tags: ''
                html: false
              element_type: ''
              element_class: ''
              element_label_type: ''
              element_label_class: ''
              element_label_colon: false
              element_wrapper_type: ''
              element_wrapper_class: ''
              element_default_classes: true
              empty: ''
              hide_empty: false
              empty_zero: false
              hide_alter_empty: true
              click_sort_column: value
              type: string
              settings: {  }
              group_column: value
              group_columns: {  }
              group_rows: true
              delta_limit: 0
              delta_offset: 0
              delta_reversed: false
              delta_first_last: false
              multi_type: separator
              separator: ', '
              field_api_classes: false
          pager:
            type: mini
            options:
              offset: 0
              pagination_heading_level: h4
              items_per_page: 10
              total_pages: null
              id: 0
              tags:
                next: β€Ίβ€Ί
                previous: β€Ήβ€Ή
              expose:
                items_per_page: false
                items_per_page_label: 'Items per page'
                items_per_page_options: '5, 10, 25, 50'
                items_per_page_options_all: false
                items_per_page_options_all_label: '- All -'
                offset: false
                offset_label: Offset
          exposed_form:
            type: basic
            options:
              submit_button: Apply
              reset_button: false
              reset_button_label: Reset
              exposed_sorts_label: 'Sort by'
              expose_sort_order: true
              sort_asc_label: Asc
              sort_desc_label: Desc
          access:
            type: none
            options: {  }
          cache:
            type: tag
            options: {  }
          empty: {  }
          sorts: {  }
          arguments: {  }
          filters: {  }
          style:
            type: default
            options:
              grouping: {  }
              row_class: ''
              default_row_class: true
              uses_fields: false
          row:
            type: fields
            options:
              default_field_elements: true
              inline: {  }
              separator: ''
              hide_empty: false
          query:
            type: views_query
            options:
              query_comment: ''
              disable_sql_rewrite: false
              distinct: false
              replica: false
              query_tags: {  }
          relationships:
            state_province_id:
              id: state_province_id
              table: civicrm_address
              field: state_province_id
              relationship: none
              group_type: group
              admin_label: 'CiviCRM State/Province'
              entity_type: civicrm_address
              entity_field: state_province_id
              plugin_id: standard
              required: false
          header: {  }
          footer: {  }
          display_extenders: {  }
        cache_metadata:
          max-age: -1
          contexts:
            - 'languages:language_content'
            - 'languages:language_interface'
            - url.query_args
          tags: {  }
    
    
  • πŸ‡¨πŸ‡¦Canada adixon

    Thanks for that! Sounds like a generally applicable strategy with similar issues I've had in the past.

  • πŸ‡ΊπŸ‡ΈUnited States markusa

    This is merged and included in the 4.0.1 release

  • πŸ‡ΊπŸ‡ΈUnited States markusa
  • Now that this issue is closed, please review the contribution record.

    As a contributor, attribute any organization helped you, or if you volunteered your own time.

    Maintainers, please credit people who helped resolve this issue.

Production build 0.71.5 2024