How to add block template suggestions? I want to be able to create a block or place a block into a region and theme it according to the selected view mode from Panels. I have noticed a discrepancy between Custom Block and Entity Block where one form gives you the view mode to show the block in, and the other does not (Custom Block).

I want the template suggestion to be along the lines of block--provider/module--block-bundle--view-mode.html.twig but struggling to find consistency in the variables in hook_theme_suggestions_block_alter() in order to add the suggestion. At the same time, I want to enforce the template that is used no matter if the user created the block inline, or referenced an existing block via Entity Block. What keys or identifiers can I look for to make this work?
Normally a content entity has a template, like node.html.twig for node, so for custom blocks there should be block-content.html.twig. To avoid overhead it was decided to remove this template. This means you can't add template suggestions to the entity template, instead you have to add them to the block template. In this case you get the variables from the plugin when adding suggestions and you don't find the view mode or other details in a consistent way like for entities.

Additionally you use two different plugins to place the block:


Original block plugin for Custom Blocks (core)

http://cgit.drupalcode.org/drupal/tree/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php?h=8.4.x
Block plugin from Entity Block (contrib)

http://cgit.drupalcode.org/entity_block/tree/src/Plugin/Block/EntityBlock.php?h=8.x-1.x


The second method, this is using custom blocks outside of the core block content plugin is not officially supported, because the pair of both are optimized to work together.

For the second method a custom entity might be a better choice (then of course one which has a template, which is standard for entities built by Drupal Console ).




  I have noticed a discrepancy between Custom Block and Entity Block
  where one form gives you the view mode to show the block in, and the
  other does not (Custom Block).


Checking the linked code from the Custom Block plugin. It should show the view mode, if there is more than one to choose from:

$form['view_mode'] = [
  '#type' =&gt; 'select',
  '#options' =&gt; $options,
  '#title' =&gt; $this-&gt;t('View mode'),
  '#description' =&gt; $this-&gt;t('Output the block in this view mode.'),
  '#default_value' =&gt; $this-&gt;configuration['view_mode'],
  '#access' =&gt; (count($options) &gt; 1),
];