Skip to content

Instantly share code, notes, and snippets.

@DuaelFr
Last active January 15, 2018 15:19
Show Gist options
  • Save DuaelFr/d93091b1dc4908562eff3e8fdb39feef to your computer and use it in GitHub Desktop.
Save DuaelFr/d93091b1dc4908562eff3e8fdb39feef to your computer and use it in GitHub Desktop.
Webform in a Paragraph (D8)
<?php
use Drupal\webform\Entity\Webform;
/**
* Implements hook_entity_extra_field_info().
*/
function MY_MODULE_entity_extra_field_info() {
$extra['paragraph']['PARAGRAPH_TYPE']['display']['FIELD_NAME'] = [
'label' => t('Form'),
'weight' => 0,
'visible' => FALSE,
];
return $extra;
}
/**
* Implements hook_entity_view().
*/
function MY_MODULE_paragraph_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
if ($display->getComponent('FIELD_NAME') && ($webform = Webform::load('WEBFORM_ID'))) {
$build['FIELD_NAME'] = [
'#type' => 'container',
'#attributes' => ['class' => ['field', 'field--name-extra-FIELD-NAME']],
'title' => [
'#type' => 'html_tag',
'#tag' => 'h2',
0 => ['#markup' => $webform->label()],
],
'content' => [
'#type' => 'webform',
'#webform' => $webform,
'#default_data' => [],
]
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment