Adding restrictions to layout options?

Created on 1 February 2024, 5 months ago
Updated 19 February 2024, 4 months ago

Problem/Motivation

How would I add a user role restriction to a layout option? We are using layout_paragraphs and are applying options to a one column section. Ideally we will limit a background color for the section only to an administrator.

I've tried multiple approaches with hook_form_alter to do this and I cannot seem to find where to make the change.

One of the options that looked promising was the following code in a hook.
$layoutManager = \Drupal::service('plugin.manager.core.layout');
$layoutInstance = $layoutManager->createInstance('layout_onecol');
$definitions = $layoutInstance->getLayoutDefinitions();

I'd then try to unset $definitions['background'] but that doesn't work.

Should I be writing my own plugin based off of layout_options to accomplish the above.

Thank you in advance for your time.

πŸ’¬ Support request
Status

Fixed

Version

1.5

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States pbabin

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

Comments & Activities

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

    One finally worked. I threw the following into a .module file. Very specific but may help others if they come across a similar situation where trying to implement layout_options using layout_paragraphs.

    Chees.

    /**
     * Implements hook_form_layout_paragraphs_component_form_alter().
     */
    function my_module_form_layout_paragraphs_component_form_alter(array &$form, FormStateInterface &$form_state) {
      // Get title of paragraph form.
      $paragraphType = $form["#title"]->getArguments()["@type"];
      // Section one column limit options based on role.
      if ($paragraphType === 'Section') {
        $form['#after_build'][] = 'uclahs_custom_paragraph_after_build';
      }
    }
    
    /**
     * After build callback for layout options in layout paragraph.
     * Extend and generalize as needed if future layout options restriction needed.
     *
     * @param array $element
     *   The form element array.
     * @param \Drupal\Core\Form\FormStateInterface $form_state
     *   The Form state.
     *
     * @return array
     *   The modified form element array.
     */
    function my_module_paragraph_after_build(array $element, FormStateInterface $form_state): array {
      $current_user = Drupal::currentUser();
      $user_roles = $current_user->getRoles();
      $needles = ['role_1', 'role_2', 'role_3'];
      if ($current_user->id() !== '1' && !array_intersect($needles, $user_roles)) {
        $element["layout_paragraphs"]["config"]["layout"]["background"]['#access'] = false;
      }
      return $element;
    }
  • Status changed to Fixed 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States pbabin
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Hi,

    Nice that you found yourself your solution.

    Yes, it is either custom plugin or a hook_form_alter.

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

Production build 0.69.0 2024