|  | 
| 8 | 8 | use Drupal\Core\Layout\LayoutDefinition; | 
| 9 | 9 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | 
| 10 | 10 | use Drupal\Core\Plugin\PluginFormInterface; | 
|  | 11 | +use Drupal\Core\Render\Element; | 
| 11 | 12 | use Drupal\ui_patterns\UiPatternsManager; | 
| 12 | 13 | use Drupal\Core\Render\ElementInfoManagerInterface; | 
| 13 | 14 | use Symfony\Component\DependencyInjection\ContainerInterface; | 
| @@ -94,12 +95,22 @@ public function build(array $regions) { | 
| 94 | 95 |       $fields[$region_name] = $regions[$region_name]; | 
| 95 | 96 |     } | 
| 96 | 97 | 
 | 
| 97 |  | -    return [ | 
|  | 98 | +    $build = [ | 
| 98 | 99 |       '#type' => 'pattern', | 
| 99 | 100 |       '#id' => $this->getPluginDefinition()->get('additional')['pattern'], | 
| 100 | 101 |       '#fields' => $fields, | 
| 101 | 102 |       '#variant' => $configuration['pattern']['variant'], | 
| 102 | 103 |     ] + $this->elementInfo->getInfo('pattern'); | 
|  | 104 | + | 
|  | 105 | +    // Add the fields at the root of the render array so modules that usually | 
|  | 106 | +    // deal with layouts (eg. Field Group) can still do their jobs. | 
|  | 107 | +    $build += $fields; | 
|  | 108 | + | 
|  | 109 | +    // Prepend a new pre_render method that will copy back altered field arrays | 
|  | 110 | +    // to the #fields variable. | 
|  | 111 | +    array_unshift($build['#pre_render'], [get_class($this), 'preProcessFields']); | 
|  | 112 | + | 
|  | 113 | +    return $build; | 
| 103 | 114 |   } | 
| 104 | 115 | 
 | 
| 105 | 116 |   /** | 
| @@ -186,4 +197,22 @@ protected function processOnlyContentFields(array &$regions) { | 
| 186 | 197 |     } | 
| 187 | 198 |   } | 
| 188 | 199 | 
 | 
|  | 200 | +  /** | 
|  | 201 | +   * Copy field content back where it belongs. | 
|  | 202 | +   * | 
|  | 203 | +   * @param array $element | 
|  | 204 | +   *   Render array. | 
|  | 205 | +   * | 
|  | 206 | +   * @return array | 
|  | 207 | +   *   Render array. | 
|  | 208 | +   */ | 
|  | 209 | +  public static function preProcessFields(array $element) { | 
|  | 210 | +    foreach ($element['#fields'] as $field_name => $content) { | 
|  | 211 | +      if (array_key_exists($field_name, $element)) { | 
|  | 212 | +        $element['#fields'][$field_name] = $element[$field_name]; | 
|  | 213 | +      } | 
|  | 214 | +    } | 
|  | 215 | +    return $element; | 
|  | 216 | +  } | 
|  | 217 | + | 
| 189 | 218 | } | 
0 commit comments