You are here

public function SimpleSiteSettingsBlock::build in Site Settings and Labels 8

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException Thrown if the entity type doesn't exist.

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Exception Thrown if the storage handler couldn't be loaded.

Overrides BlockPluginInterface::build

File

src/Plugin/Block/SimpleSiteSettingsBlock.php, line 118

Class

SimpleSiteSettingsBlock
Provides a 'SimpleSiteSettingsBlock' block.

Namespace

Drupal\site_settings\Plugin\Block

Code

public function build() {
  $build = [];
  $base_fields = [];

  // Get the renderer for a basic rendering. Users can use the templating
  // system to do something more advanced.
  $this->siteSettingsRender
    ->setDefaultImageSizeOutput(400, 400);

  // Get all settings in the selected bundle.
  $entities = $this->entityTypeManager
    ->getStorage('site_setting_entity')
    ->loadByProperties([
    'type' => $this->configuration['setting'],
  ]);
  if (empty($entities)) {
    return $build;
  }

  // Loop through the entities and their fields.
  foreach ($entities as $entity) {

    /** @var \Drupal\site_settings\SiteSettingEntityInterface $entity */

    // Determine which fields to exclude from render.
    if (!$base_fields) {
      $base_fields = array_keys($entity
        ->getEntityType()
        ->getKeys());
      $base_fields = array_merge($base_fields, [
        'name',
        'user_id',
        'type',
        'created',
        'changed',
      ]);
    }
    $fields = $entity
      ->getFields();
    foreach ($fields as $key => $field) {

      // Exclude base fields from output.
      if (!in_array($key, $base_fields) && method_exists(get_class($field), 'getFieldDefinition')) {
        $build[] = [
          '#markup' => $this->siteSettingsRender
            ->renderField($field),
        ];
      }
    }
  }
  return $build;
}