Problem/Motivation

There are often very meaty vocabularies and maybe you only need to filter by parents.

Proposed resolution

I've added a "Show only parents" option inside the SettingsForm. If enabled the permissions_by_term_form_user_form_alter function, during all terms obtaining, will filter the list for all terms with parent = 0 (that means that has no parents).

User interface changes

Just another checkbox into the Settings page. UI Changin screen

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

reinchek created an issue. See original summary.

reinchek’s picture

StatusFileSize
new70.04 KB
reinchek’s picture

Issue summary: View changes
reinchek’s picture

Just a brief description of what i did inside the permissions_by_terms_parents.patch

- Added only_parents item inside $form array, SettingsForm.php:a

    $form['only_parents'] = [
      '#type'          => 'checkbox',
      '#title'         => t('Show only the parent terms'),
      '#description'   => t('Check this if you doesn\'t want to show terms children but only the parents.'),
      '#default_value' => $this->config('permissions_by_term.settings')->get('only_parents') ?? false
    ];
...
     $this->configFactory
      ->getEditable('permissions_by_term.settings')
      ->set('only_parents', $form_state->getValue('only_parents'))
      ->save();

- Then, before getting all terms (from multiple vocabularies or from one) checks if the only_parents is true (default value FALSE):

  ...
  $settings = \Drupal::config('permissions_by_term.settings');
  $target_bundles = $settings->get('target_bundles');
  $only_parents   = $settings->get('only_parents');
  $terms = $target_bundles
    ? $term_storage->loadByProperties(['vid' => $target_bundles] + ($only_parents ? ['parent' => 0] : []))
    : ($only_parents ? $term_storage->loadByProperties(['parent' => 0]) : $term_storage->loadMultiple());
  ...
reinchek’s picture

marcoliver made their first commit to this issue’s fork.

marcoliver’s picture

Hi reinchek, thanks for your patch!

I went ahead and created an issue fork. The patch applied fine. I made some slight modifications (using $this->t() throughout the settings form now; made the description of the checkbox a bit more self-explanatory).

Feel free to take a look if you are happy with these changes.

reinchek’s picture

Hi @marcoliver,
I fully agree with the proposed changes (I thought I had applied them myself, but apparently I mixed them up when creating the patch). Indeed, thank you for taking the trouble to look and fix those unclean portions of code!

  • marcoliver committed 93585103 on 3.1.x-dev
    Issue #3316218 by marcoliver, reinchek: Add an option to get only parent...
marcoliver’s picture

Status: Needs review » Fixed

Thanks! I merged the changes just now. New release forthcoming.

reinchek’s picture

Great! thank you!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.