You are here

function theme_entity_reference_layout_radio in Entity Reference with Layout 8

Implements hook_entity_reference_layout_radio().

Custom theme hook for adding layout icons and wrapper HTML to layout select radios.

1 string reference to 'theme_entity_reference_layout_radio'
entity_reference_layout_theme in ./entity_reference_layout.module
Implements hook_theme().

File

./entity_reference_layout.module, line 70
Contains entity_reference_layout.module.

Code

function theme_entity_reference_layout_radio($element) {

  /* @var \Drupal\Core\Layout\LayoutPluginManager $layout_plugin_manager */
  $layout_plugin_manager = \Drupal::service('plugin.manager.core.layout');
  $renderer = \Drupal::service('renderer');
  $layout_name = $element['element']['#return_value'];
  try {

    /* @var \Drupal\Core\Layout\LayoutDefinition $definition */
    $definition = $layout_plugin_manager
      ->getDefinition($layout_name);
    $icon = $definition
      ->getIcon(40, 60, 1, 0);
    $rendered_icon = $renderer
      ->render($icon);
    $layout_item = [
      '#type' => 'container',
      '#prefix' => '<div class="layout-radio-item">',
      '#suffix' => '</div>',
      'icon' => [
        '#prefix' => '<div class="layout-icon-wrapper">',
        '#suffix' => '</div>',
        '#markup' => $rendered_icon,
      ],
      'radio' => [
        '#type' => 'container',
        '#attributes' => [],
        'item' => [
          '#markup' => $element['element']['#children'],
        ],
      ],
    ];
    return \Drupal::service('renderer')
      ->render($layout_item);
  } catch (\Exception $e) {
    watchdog_exception('entity_reference_layout', $e);
  }
  return [];
}