Last active
May 28, 2020 16:04
-
-
Save fredysan/4a2a0309608236d0ce7074f9032a6227 to your computer and use it in GitHub Desktop.
Navigate through Layout Builder Components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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