getInstances example

/** * Provides a confirmation form for deleting a content block entity. * * @internal */
class BlockContentDeleteForm extends ContentEntityDeleteForm {

  /** * {@inheritdoc} */
  public function getDescription() {
    $instances = $this->entity->getInstances();
    if (!empty($instances)) {
      return $this->formatPlural(count($instances), 'This will also remove 1 placed block instance. This action cannot be undone.', 'This will also remove @count placed block instances. This action cannot be undone.');
    }
    return parent::getDescription();
  }

}

  }

  /** * {@inheritdoc} */
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    parent::preDelete($storage$entities);

    /** @var \Drupal\block_content\BlockContentInterface $block */
    foreach ($entities as $block) {
      foreach ($block->getInstances() as $instance) {
        $instance->delete();
      }
    }
  }

  /** * {@inheritdoc} */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage$entities);
    /** @var \Drupal\block_content\BlockContentInterface $block */
    
'settings[label]' => $edit['info[0][value]'],
      'region' => 'sidebar_first',
    ];
    $block = BlockContent::load(1);
    $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
    $this->drupalGet($url);
    $this->submitForm($instance, 'Save block');

    $block = BlockContent::load(1);

    // Test getInstances method.     $this->assertCount(1, $block->getInstances());

    // Navigate to home page.     $this->drupalGet('');
    $this->assertSession()->pageTextContains($body);

    // Delete the block.     $this->drupalGet('admin/content/block/1/delete');
    $this->assertSession()->pageTextContains('This will also remove 1 placed block instance.');

    $this->submitForm([], 'Delete');
    $this->assertSession()->pageTextContains('The content block ' . $edit['info[0][value]'] . ' has been deleted.');

    
Home | Imprint | This part of the site doesn't use cookies.