You are here

public function EntityEditFormBlock::blockForm in Entityform block 8

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/EntityEditFormBlock.php, line 94

Class

EntityEditFormBlock
Provides a block for creating a new content entity.

Namespace

Drupal\entityform_block\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $form = parent::blockForm($form, $form_state);

  // Get content entity types.

  /** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $content_entity_types */
  $content_entity_types = [];
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $entity_type_definition) {
    if ($entity_type_definition
      ->getGroup() == 'content') {
      $content_entity_types[$entity_type_id] = $entity_type_definition;
    }
  }
  $options = array();
  foreach ($content_entity_types as $type_key => $entity_type) {

    // Entities that do not declare a form class.
    // Exclude Comment entities as they have to be attached to another entity.
    if (!$entity_type
      ->hasFormClasses() || $type_key == 'comment') {
      continue;
    }

    // Get all bundles for current entity type.
    $entity_type_bundles = $this->entityTypeBundleInfo
      ->getBundleInfo($type_key);
    foreach ($entity_type_bundles as $bundle_key => $bundle_info) {

      // Personal contact form requires a user recipient to be specified.
      if ($bundle_key == 'personal' && $type_key == 'contact_message') {
        continue;
      }
      $options[(string) $entity_type
        ->getLabel()][$type_key . '.' . $bundle_key] = $bundle_info['label'];
    }
  }
  $form['entity_type_bundle'] = array(
    '#title' => $this
      ->t('Entity Type + Bundle'),
    '#type' => 'select',
    '#options' => $options,
    '#required' => TRUE,
    '#default_value' => $this->configuration['entity_type'] . '.' . $this->configuration['bundle'],
  );
  return $form;
}