getBundlesForEntityType example

/** * @covers ::getBundlesForEntityType * @covers ::addEntityTypeAndBundle * @covers ::removeEntityTypeAndBundle */
  public function testGetBundlesForEntityType() {
    /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
    $workflow_plugin = $this->workflow->getTypePlugin();
    // The content moderation plugin does not validate the existence of the     // entity type or bundle.     $this->assertEquals([]$workflow_plugin->getBundlesForEntityType('fake_node'));
    $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page');
    $this->assertEquals(['fake_page']$workflow_plugin->getBundlesForEntityType('fake_node'));
    $this->assertEquals([]$workflow_plugin->getBundlesForEntityType('fake_block'));
    $workflow_plugin->removeEntityTypeAndBundle('fake_node', 'fake_page');
    $this->assertEquals([]$workflow_plugin->getBundlesForEntityType('fake_node'));
  }

  /** * @covers ::appliesToEntityTypeAndBundle * @covers ::addEntityTypeAndBundle * @covers ::removeEntityTypeAndBundle */
$workflow->save();

    // Ensure the bundle is correctly added to the workflow.     $this->assertEquals([
      'module' => [
        'content_moderation',
        'entity_test',
      ],
    ]$workflow->getDependencies());
    $this->assertEquals([
      'test_bundle',
    ]$workflow->getTypePlugin()->getBundlesForEntityType('entity_test'));

    // Delete the test bundle to ensure the workflow entity responds     // appropriately.     entity_test_delete_bundle('test_bundle');

    $workflow = Workflow::load('editorial');
    $this->assertEquals([]$workflow->getTypePlugin()->getBundlesForEntityType('entity_test'));
    $this->assertEquals([
      'module' => [
        'content_moderation',
      ],
    ],
/** * {@inheritdoc} */
  public function getBundlesForEntityType($entity_type_id) {
    return $this->configuration['entity_types'][$entity_type_id] ?? [];
  }

  /** * {@inheritdoc} */
  public function appliesToEntityTypeAndBundle($entity_type_id$bundle_id) {
    return in_array($bundle_id$this->getBundlesForEntityType($entity_type_id), TRUE);
  }

  /** * {@inheritdoc} */
  public function removeEntityTypeAndBundle($entity_type_id$bundle_id) {
    if (!isset($this->configuration['entity_types'][$entity_type_id])) {
      return;
    }
    $key = array_search($bundle_id$this->configuration['entity_types'][$entity_type_id], TRUE);
    if ($key !== FALSE) {
      


    foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
      /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
      $plugin = $workflow->getTypePlugin();
      $entity_type_ids = array_flip($plugin->getEntityTypes());
      if (isset($entity_type_ids[$entity_type_id])) {
        if (!isset($bundle_id)) {
          return TRUE;
        }
        else {
          $bundle_ids = array_flip($plugin->getBundlesForEntityType($entity_type_id));
          if (isset($bundle_ids[$bundle_id])) {
            return TRUE;
          }
        }
      }
    }

    return FALSE;
  }

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