<?php

namespace Drupal\mymodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides
 *
 * @Block(
 *   id = "test_context_definition",
 *   admin_label = @Translation("Test block with context_definition"),
 *   context_definitions = {
 *     "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node")),
 *     "term" = @ContextDefinition("entity:taxonomy_term", required = FALSE, label = @Translation("Term")),
 *   }
 * )
 */

class ContextDefinition extends BlockBase {

  public function build() {
    $node = $this->getContextValue('node');
    $term = $this->getContextValue('term');
    $build = ['#markup' => 'Hello, From mymodule/Block'];
    if (!empty($node) or !empty($term)) {
      empty($node) ? $entiy = $term : $entiy = $node;
      empty($node) ? $entiy_type_id = 'taxonomy_term' : $entiy_type_id  = 'node';
      $build = \Drupal::entityTypeManager()->getViewBuilder($entiy_type_id)->view($entiy, 'teaser');
      $tags = $entiy->getCacheTags();
      foreach ($tags as $tag) {
        $build['#cache']['tags'][] = $tag;
      }
    }

    return $build;
  }
}
