Advertising sustains the DA. Ads are hidden for members. Join today

Code snippets

Last updated on
22 August 2024

How to check if some paragraph has specific value selected for a specific plugin style.

/**
 * This example is for a link paragraph type whic has button_size style enabled.
 */
function custom_theme_preprocess_paragraph__link(&$variables) {
  /** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
  $paragraph = $variables['paragraph'];
  // Does this paragraph has style plugin enabled.
  if ($paragraph->getParagraphType()->hasEnabledBehaviorPlugin('style')) {
    // Get style plugin instance for this paragraph.
    $style_plugin = $paragraph->getParagraphType()->getBehaviorPlugin('style');
    // Get all plugin styles data for this paragraph.  
    $styles = $style_plugin->getStyles($paragraph);
    // Check if paragraph has button_size plugin with a value of button-width-full.
    if (isset($styles['button_size']) && $styles['button_size'] === 'button-width-full') {
      // Do something...
    }
  }
}

  

Help improve this page

Page status: No known problems

You can: