Skip to content

Instantly share code, notes, and snippets.

@fredysan
Last active May 28, 2020 16:04
Show Gist options
  • Save fredysan/4a2a0309608236d0ce7074f9032a6227 to your computer and use it in GitHub Desktop.
Save fredysan/4a2a0309608236d0ce7074f9032a6227 to your computer and use it in GitHub Desktop.
Navigate through Layout Builder Components
<?php
namespace Drupal\module\Service;
use Drupal\node\NodeInterface;
/**
* Helper Class.
*/
class Helper {
/**
* Gets the Id of the first Webform Component inside Layout Builder.
*
* @param \Drupal\node\NodeInterface $node
* Token data argument.
*
* @return string
* Webform ID.
*/
public function getWebformId(NodeInterface $node) : string {
if (!$node->hasField('layout_builder__layout')) {
return '';
}
foreach ($node->get('layout_builder__layout') as $item) {
$components = $item->get('section')->getValue()->getComponents();
foreach ($components as $component) {
/** @var \Drupal\layout_builder\SectionComponent $component */
if (isset($component->toArray()['configuration']['webform_id'])) {
return str_replace('_', '-', $component->toArray()['configuration']['webform_id']);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment