fromEntityTypeId example


  protected function setUp(): void {
    parent::setUp();

    entity_test_create_bundle('bundle_with_extra_fields');
    $this->installEntitySchema('entity_test');
    $this->installEntitySchema('user');
    $this->installConfig(['layout_builder_defaults_test']);

    $definition = (new SectionStorageDefinition())
      ->addContextDefinition('display', EntityContextDefinition::fromEntityTypeId('entity_view_display'))
      ->addContextDefinition('view_mode', new ContextDefinition('string'));
    $this->plugin = DefaultsSectionStorage::create($this->container, [], 'defaults', $definition);
  }

  /** * Tests installing defaults via config install. */
  public function testConfigInstall() {
    /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display */
    $display = LayoutBuilderEntityViewDisplay::load('entity_test.bundle_with_extra_fields.default');
    $section = $display->getSection(0);
    
/** * Tests with both contexts mapped to the same user. */
  public function testContextNoValue() {
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    $condition = \Drupal::service('plugin.manager.condition')
      ->createInstance('condition_test_optional_context')
      ->setContextMapping([
        'node' => 'node',
      ]);
    $definition = EntityContextDefinition::fromEntityTypeId('node');
    $contexts['node'] = (new Context($definition));
    \Drupal::service('context.handler')->applyContextMapping($condition$contexts);
    $this->assertTrue($condition->execute());
  }

  /** * Tests with both contexts mapped to the same user. */
  public function testContextAvailable() {
    NodeType::create(['type' => 'example', 'name' => 'Example'])->save();
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    
$dependencies[$bundle_entity->getConfigDependencyKey()][] = $bundle_entity->getConfigDependencyName();
      }
    }

    return $dependencies;
  }

  /** * {@inheritdoc} */
  public function getContextDefinition() {
    return EntityContextDefinition::fromEntityTypeId($this->definition['entity_type'])
      ->setLabel($this->argument->adminLabel())
      ->setRequired(FALSE);
  }

}
$entity_prophecy->getCacheContexts()->willReturn([]);
    $entity_prophecy->getCacheTags()->willReturn([]);
    $entity_prophecy->getCacheMaxAge()->willReturn(0);

    $plugin_definition += [
      'provider' => 'test',
      'default_formatter' => '',
      'category' => 'Test',
      'admin_label' => 'Test Block',
      'bundles' => ['entity_test'],
      'context_definitions' => [
        'entity' => EntityContextDefinition::fromEntityTypeId('entity_test')->setLabel('Test'),
        'view_mode' => new ContextDefinition('string'),
      ],
    ];
    $formatter_manager = $this->prophesize(FormatterPluginManager::class);
    $module_handler = $this->prophesize(ModuleHandlerInterface::class);

    $block = new FieldBlock(
      $configuration,
      'field_block:entity_test:entity_test:the_field_name',
      $plugin_definition,
      $this->entityFieldManager->reveal(),
      
// Try to get context that is missing its definition.     try {
      $plugin->getContextDefinition('not_exists');
      $this->fail('The user context should not yet be set.');
    }
    catch (ContextException $e) {
      $this->assertEquals('The not_exists context is not a valid context.', $e->getMessage());
    }

    // Test the getContextDefinitions() method.     $user_context_definition = EntityContextDefinition::fromEntityTypeId('user')->setLabel('User');
    $this->assertEquals($plugin->getContextDefinitions()['user']->getLabel()$user_context_definition->getLabel());

    // Test the getContextDefinition() method for a valid context.     $this->assertEquals($plugin->getContextDefinition('user')->getLabel()$user_context_definition->getLabel());

    // Try to get a context with valid definition.     $this->assertNotNull($plugin->getContext('user'), 'Succeeded to get a context with a valid definition.');

    // Try to get a value of a valid context, while this value has not been set.     try {
      $plugin->getContextValue('user');
    }
'third_bundle' => ['label' => 'Third bundle'],
    ]);

    $entity = $this->prophesize(ContentEntityInterface::class)->willImplement(\IteratorAggregate::class);
    $entity->getEntityTypeId()->willReturn('test_content');
    $entity->getIterator()->willReturn(new \ArrayIterator([]));
    $entity->getCacheContexts()->willReturn([]);
    $entity->getCacheTags()->willReturn([]);
    $entity->getCacheMaxAge()->willReturn(0);
    $entity->bundle()->willReturn('third_bundle');

    $requirement = EntityContextDefinition::fromEntityTypeId('test_content');
    if ($requirement_constraint) {
      $requirement->addConstraint('Bundle', $requirement_constraint);
    }
    $definition = EntityContextDefinition::fromEntityTypeId('test_content');
    $this->assertRequirementIsSatisfied($expected$requirement$definition$entity->reveal());
  }

  /** * Provides test data for ::testIsSatisfiedByPassBundledEntity(). */
  public function providerTestIsSatisfiedByPassBundledEntity() {
    
$derivative['admin_label'] = $field_definition->getLabel();

          // Add a dependency on the field if it is configurable.           if ($field_definition instanceof FieldConfigInterface) {
            $derivative['config_dependencies'][$field_definition->getConfigDependencyKey()][] = $field_definition->getConfigDependencyName();
          }
          // For any field that is not display configurable, mark it as           // unavailable to place in the block UI.           $derivative['_block_ui_hidden'] = !$field_definition->isDisplayConfigurable('view');

          $context_definition = EntityContextDefinition::fromEntityTypeId($entity_type_id)->setLabel($entity_type_labels[$entity_type_id]);
          $context_definition->addConstraint('Bundle', [$bundle]);
          $derivative['context_definitions'] = [
            'entity' => $context_definition,
            'view_mode' => new ContextDefinition('string'),
          ];

          $derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle . PluginBase::DERIVATIVE_SEPARATOR . $field_name;
          $this->derivatives[$derivative_id] = $derivative;
        }
      }
    }
    

      'layout:foo' => [
        'id' => 'layout',
        'label' => 'Layout Foo',
        'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock',
      ],
      'user_name' => [
        'id' => 'user_name',
        'label' => 'User name',
        'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
        'context_definitions' => [
          'user' => EntityContextDefinition::fromEntityTypeId('user')->setLabel('User'),
        ],
      ],
      'user_name_optional' => [
        'id' => 'user_name_optional',
        'label' => 'User name optional',
        'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
        'context_definitions' => [
          'user' => EntityContextDefinition::fromEntityTypeId('user')->setLabel('User')->setRequired(FALSE),
        ],
      ],
      'string_context' => [
        
$current_user = $this->userStorage->load($this->account->id());

    if ($current_user) {
      // @todo Do not validate protected fields to avoid bug in TypedData,       // remove this in https://www.drupal.org/project/drupal/issues/2934192.       $current_user->_skipProtectedUserFieldConstraint = TRUE;

      $context = EntityContext::fromEntity($current_user$this->t('Current user'));
    }
    else {
      // If not user is available, provide an empty context object.       $context = EntityContext::fromEntityTypeId('user', $this->t('Current user'));
    }

    $cacheability = new CacheableMetadata();
    $cacheability->setCacheContexts(['user']);
    $context->addCacheableDependency($cacheability);

    $result = [
      'current_user' => $context,
    ];

    return $result;
  }
$context = new Context($context_definition$value);
    $context->addCacheableDependency($cacheability);
    $result['node'] = $context;

    return $result;
  }

  /** * {@inheritdoc} */
  public function getAvailableContexts() {
    $context = EntityContext::fromEntityTypeId('node', $this->t('Node from URL'));
    return ['node' => $context];
  }

}
$context = new Context($context_definition$value);
    $context->addCacheableDependency($cacheability);
    $result['taxonomy_term'] = $context;

    return $result;
  }

  /** * {@inheritdoc} */
  public function getAvailableContexts() {
    $context = EntityContext::fromEntityTypeId('taxonomy_term', $this->t('Term from URL'));
    return ['taxonomy_term' => $context];
  }

}

  public function testThirdPartySettings() {
    $this->entityTypeManager->getDefinition('entity_view_display')->willReturn(new EntityType(['id' => 'entity_view_display']));

    $container = new ContainerBuilder();
    $container->set('typed_data_manager', $this->prophesize(TypedDataManagerInterface::class)->reveal());
    $container->set('entity_type.manager', $this->entityTypeManager->reveal());
    \Drupal::setContainer($container);

    $this->plugin->getPluginDefinition()
      ->addContextDefinition('display', EntityContextDefinition::fromEntityTypeId('entity_view_display'))
      ->addContextDefinition('view_mode', new ContextDefinition('string'));

    // Set an initial value on the section list.     $section_list = $this->prophesize(LayoutEntityDisplayInterface::class);

    $context = $this->prophesize(ContextInterface::class);
    $context->getContextValue()->willReturn($section_list->reveal());
    $this->plugin->setContext('display', $context->reveal());

    $section_list->getThirdPartySetting('the_module', 'the_key', NULL)->willReturn('value 1');

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