How can I programmatically create a block type? I want to create a block type that has a couple of fields, a template and attached JavaScript.
I want that the user in my site will be able to create blocks of this type just like they create a custom block: through /block/add/myBlockType.
I would like ideally to have it all done programmatically but I dont mind manually creating my block type though &quot;admin/structure/block/block-content/types&quot; or with configuration files.
The thing is I have been scraping google for two days already and I don't manage to figure out how to programmatically create a block type
And this that the information is not easily found on Google makes me think that instead I need to create my type with configuration and then apply my plugin on creation of a new block but im still not sure how to do that and what my plugin needs to extend.
How can I do that?
I had the same issue and I hope I have resolved it.
Your algorithm is next:

Create your own module
Create file &lt;your_module&gt;.install
There must be at least one hook hook_install (I have used two: hook_install and hook_uninstall)
Inside install hook:

create a new block type
create fieldStorageConfig
create fieldConfig
set component for form and view display



My code:
&lt;?php

use Drupal\block_content\Entity\BlockContentType;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

function &lt;your_module_name&gt;_install() {
  $newBlockType = BlockContentType::create([
    'id' =&gt; &lt;your_id&gt;,
    'label' =&gt; 'Custom block type',
    'description' =&gt; &quot;A custom block type&quot;,
  ]);
  $newBlockType-&gt;save();

  $blockId = $newBlockType-&gt;id();

  block_content_add_field($blockId, 'Icon', 'custom_block_icon', 'image', 'image', 'image_image');
  block_content_add_field($blockId, 'Count', 'custom_block_count', 'integer', 'number_integer', 'number');
  block_content_add_field($blockId, 'Description', 'custom_block_description', 'text', 'text_default', 'text_textfield');

}
function block_content_add_field(
  $blockTypeId,
  $label,
  $componentName,
  $fieldStorageType,
  $viewDisplayType = 'text_default',
  $formDisplayType = 'text_textarea_with_summary') {
    // Add or remove the body field, as needed.
    $field = FieldConfig::loadByName('block_content', $blockTypeId, $componentName);
    if (empty($field)) {
      $fieldStorage = FieldStorageConfig::loadByName('block_content', $componentName);
      if (empty($fieldStorage)) {
        $fieldStorage = FieldStorageConfig::create([
          'field_name' =&gt; $componentName,
          'entity_type' =&gt; 'block_content',
          'type' =&gt; $fieldStorageType,
        ]);
        $fieldStorage-&gt;save();
      }

      $field = FieldConfig::create([
        'field_storage' =&gt; $fieldStorage,
        'bundle' =&gt; $blockTypeId,
        'label' =&gt; $label,
        'settings' =&gt; ['display_summary' =&gt; FALSE],
      ]);

      $field-&gt;save();

      /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
      $display_repository = \Drupal::service('entity_display.repository');

      // Assign widget settings for the default form mode.
      $display_repository-&gt;getFormDisplay('block_content', $blockTypeId)
        -&gt;setComponent($componentName, [
          'type' =&gt; $formDisplayType,
        ])
        -&gt;save();

      // Assign display settings for default view mode.
      $display_repository-&gt;getViewDisplay('block_content', $blockTypeId)
        -&gt;setComponent($componentName, [
          'label' =&gt; 'hidden',
          'type' =&gt; $viewDisplayType,
        ])
        -&gt;save();
    }

    return $field;
}

Result: the new custom block type with 3 fields.