Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 299468

Use the builder pattern to make it easier to create render arrays

$
0
0

Problem/Motivation

In Drupal 8 we've been working to remove 'magic array keys'. But we still have render arrays, which everytime you use them cause you to need to look-up the allowed keys.
This is bad for DX.

Proposed resolution

Provide builder classes for each element type to make defining render arrays discoverable from IDEs
before

<?php
$variables
['table'] = array(
   
'#type'=> 'table',
   
'#header'=> $headers,
   
'#rows'=> $rows,
   
'#attributes'=> array(
     
'id'=> $table_id,
    ),
   
'#tabledrag'=> array(
      array(
       
'action'=> 'order',
       
'relationship'=> 'sibling',
       
'group'=> $weight_class,
      ),
    ),
  );
?>

after

<?php
$variables
['table'] = TableElement::create()
    ->
setHeader($headers)
    ->
setRows($rows)
    ->
setAttributes(array(
     
'id'=> $table_id,
    ))
    ->
setTableDrag(array(
      array(
       
'action'=> 'order',
       
'relationship'=> 'sibling',
       
'group'=> $weight_class,
      ),
    ))->
toRenderArray();
?>

Borrows heavily from both Url helper and FieldDefinition stuff.

Remaining tasks

Review

User interface changes

None

API changes

Only additions.


Viewing all articles
Browse latest Browse all 299468

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>