isRequired example

// Test field storage.     $this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.');
    $this->assertFalse($field_storage->isLocked(), 'Field storage is not locked.');
    $this->assertSame('string', $field_storage->getType(), 'Field is not of correct type.');
    $this->assertSame('field_media_test_1', $field_storage->getName(), 'Incorrect field name is used.');
    $this->assertSame('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.');

    // Test field.     $this->assertTrue($field->isNew(), 'Field is saved automatically.');
    $this->assertSame('field_media_test_1', $field->getName(), 'Incorrect field name is used.');
    $this->assertSame('string', $field->getType(), 'Field is of incorrect type.');
    $this->assertTrue($field->isRequired(), 'Field is not required.');
    $this->assertEquals('Test source', $field->label(), 'Incorrect label is used.');
    $this->assertSame('test_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.');

    // Fields should be automatically saved only when creating the media type     // using the media type creation form. Make sure that they are not saved     // when creating a media type programmatically.     // Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest is testing     // form part of the functionality.     $type->save();
    $storage = FieldStorageConfig::load('media.field_media_test_1');
    $this->assertNull($storage, 'Field storage was not saved.');
    
$bundles = $field_map[$this->entityTypeId][$field_name]['bundles'];

    // Sort bundles to ensure deterministic behavior.     sort($bundles);
    $existing_bundle = reset($bundles);

    // Copy field configuration.     $existing_field = $this->entityFieldManager->getFieldDefinitions($this->entityTypeId, $existing_bundle)[$field_name];
    $default_options['field_config'] = [
      'description' => $existing_field->getDescription(),
      'settings' => $existing_field->getSettings(),
      'required' => $existing_field->isRequired(),
      'default_value' => $existing_field->getDefaultValueLiteral(),
      'default_value_callback' => $existing_field->getDefaultValueCallback(),
    ];

    // Copy form and view mode configuration.     $properties = [
      'targetEntityType' => $this->entityTypeId,
      'bundle' => $existing_bundle,
    ];
    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $existing_forms */
    $existing_forms = $this->entityTypeManager->getStorage('entity_form_display')->loadByProperties($properties);
    
$this->assertEquals(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, $definition->getCardinality());
  }

  /** * Tests required. * * @covers ::isRequired * @covers ::setRequired */
  public function testFieldRequired() {
    $definition = BaseFieldDefinition::create($this->fieldType);
    $this->assertFalse($definition->isRequired());
    $definition->setRequired(TRUE);
    $this->assertTrue($definition->isRequired());
    $definition->setRequired(FALSE);
    $this->assertFalse($definition->isRequired());
  }

  /** * Tests storage required. * * @covers ::isStorageRequired * @covers ::setStorageRequired */
$type_definition = $this->getDefinition($definition->getDataType());
    // Auto-generate a constraint for data types implementing a primitive     // interface.     if (is_subclass_of($type_definition['class'], '\Drupal\Core\TypedData\PrimitiveInterface')) {
      $constraints['PrimitiveType'] = [];
    }
    // Add in constraints specified by the data type.     if (isset($type_definition['constraints'])) {
      $constraints += $type_definition['constraints'];
    }
    // Add the NotNull constraint for required data.     if ($definition->isRequired()) {
      $constraints['NotNull'] = [];
    }
    // Check if the class provides allowed values.     if (is_subclass_of($definition->getClass(), 'Drupal\Core\TypedData\OptionsProviderInterface')) {
      $constraints['AllowedValues'] = [];
    }
    return $constraints;
  }

  /** * {@inheritdoc} */
foreach ($cookieGroup['cookies'] as $cookie) {
                if ($cookie['name'] !== $foundCookie->getName()) {
                    continue;
                }

                try {
                    $cookieGroupStruct = $this->cookieGroupCollection->getGroupByName($cookieGroup['name']);
                } catch (NoCookieGroupByNameKnownException $e) {
                    continue;
                }

                return $cookieGroupStruct->isRequired() ?: $cookie['active'];
            }
        }

        return $this->getTechnicallyRequiredCookies()->hasCookieWithName($cookieName);
    }
}

    public function addArgument(InputArgument $argument)
    {
        if (isset($this->arguments[$argument->getName()])) {
            throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
        }

        if (null !== $this->lastArrayArgument) {
            throw new LogicException(sprintf('Cannot add a required argument "%s" after an array argument "%s".', $argument->getName()$this->lastArrayArgument->getName()));
        }

        if ($argument->isRequired() && null !== $this->lastOptionalArgument) {
            throw new LogicException(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $argument->getName()$this->lastOptionalArgument->getName()));
        }

        if ($argument->isArray()) {
            $this->lastArrayArgument = $argument;
        }

        if ($argument->isRequired()) {
            ++$this->requiredCount;
        } else {
            $this->lastOptionalArgument = $argument;
        }
    // field type option.     $this->assertSession()->optionExists('edit-new-storage-type', 'field_ui:test_field_with_preconfigured_options:custom_options');
    $this->assertSession()->optionExists('edit-new-storage-type', 'test_field_with_preconfigured_options');

    // Add a field with every possible preconfigured value.     $this->fieldUIAddNewField(NULL, 'test_custom_options', 'Test label', 'field_ui:test_field_with_preconfigured_options:custom_options');
    $field_storage = FieldStorageConfig::loadByName('node', 'field_test_custom_options');
    $this->assertEquals(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, $field_storage->getCardinality());
    $this->assertEquals('preconfigured_storage_setting', $field_storage->getSetting('test_field_storage_setting'));

    $field = FieldConfig::loadByName('node', 'article', 'field_test_custom_options');
    $this->assertTrue($field->isRequired());
    $this->assertEquals('preconfigured_field_setting', $field->getSetting('test_field_setting'));

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

    $form_display = $display_repository->getFormDisplay('node', 'article');
    $this->assertEquals('test_field_widget_multiple', $form_display->getComponent('field_test_custom_options')['type']);
    $view_display = $display_repository->getViewDisplay('node', 'article');
    $this->assertEquals('field_test_multiple', $view_display->getComponent('field_test_custom_options')['type']);
    $this->assertEquals('altered dummy test string', $view_display->getComponent('field_test_custom_options')['settings']['test_formatter_setting_multiple']);
  }

  
class LoginLinkFactory extends AbstractFactory
{
    public const PRIORITY = -20;

    public function addConfiguration(NodeDefinition $node): void
    {
        /** @var NodeBuilder $builder */
        $builder = $node->fixXmlConfig('signature_property', 'signature_properties')->children();

        $builder
            ->scalarNode('check_route')
                ->isRequired()
                ->info('Route that will validate the login link - e.g. "app_login_link_verify".')
            ->end()
            ->scalarNode('check_post_only')
                ->defaultFalse()
                ->info('If true, only HTTP POST requests to "check_route" will be handled by the authenticator.')
            ->end()
            ->arrayNode('signature_properties')
                ->isRequired()
                ->prototype('scalar')->end()
                ->requiresAtLeastOneElement()
                ->info('An array of properties on your User that are used to sign the link. If any of these change, all existing links will become invalid.')
                


  /** * {@inheritdoc} */
  public function isStorageRequired() {
    if (isset($this->definition['storage_required'])) {
      return (bool) $this->definition['storage_required'];
    }

    // Default to the 'required' property of the base field.     return $this->isRequired();
  }

  /** * Sets whether the field storage is required. * * @param bool $required * Whether the field storage is required. * * @return static * The object itself for chaining. */
  
'administer blocks',
    ]));

    // Check if context was correctly propagated to the block.     $definition = $this->container->get('plugin.manager.block')
      ->getDefinition('views_block:test_view_block_with_context-block_1');
    $this->assertInstanceOf(ContextDefinitionInterface::class$definition['context_definitions']['nid']);
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
    $context = $definition['context_definitions']['nid'];
    $this->assertEquals('entity:node', $context->getDataType(), 'Context definition data type is correct.');
    $this->assertEquals('Content: ID', $context->getLabel(), 'Context definition label is correct.');
    $this->assertFalse($context->isRequired(), 'Context is not required.');

    // Place test block via block UI to check if contexts are correctly exposed.     $this->drupalGet(
      'admin/structure/block/add/views_block:test_view_block_with_context-block_1/stark',
      ['query' => ['region' => 'content']]
    );
    $edit = [
      'settings[context_mapping][nid]' => '@node.node_route_context:node',
    ];
    $this->submitForm($edit, 'Save block');

    
$fields = [
            [
                'name' => 'id',
                'type' => 'int',
            ],
        ];

        foreach ($type->getFields() as $field) {
            $fields[] = [
                'name' => $field->getName(),
                'type' => $field->getType()::getExtjsType(),
                'useNull' => !$field->isRequired(),
            ];
        }

        return $fields;
    }

    public function buildColumns(Type $type): array
    {
        $fields = [];

        foreach ($type->getFields() as $field) {
            


  /** * {@inheritdoc} */
  public function getContextValue() {
    // Support optional contexts.     if (!isset($this->contextValue)) {
      $definition = $this->getContextDefinition();
      $default_value = $definition->getDefaultValue();

      if (!isset($default_value) && $definition->isRequired()) {
        $type = $definition->getDataType();
        throw new ContextException(sprintf("The %s context is required and not present.", $type));
      }
      // Keep the default value here so that subsequent calls don't have to look       // it up again.       $this->contextValue = $default_value;
    }
    return $this->contextValue;
  }

  /** * {@inheritdoc} */

    protected function finalizeValue(mixed $value): mixed
    {
        if (false === $value) {
            throw new UnsetKeyException(sprintf('Unsetting key for path "%s", value: %s.', $this->getPath()json_encode($value)));
        }

        foreach ($this->children as $name => $child) {
            if (!\array_key_exists($name$value)) {
                if ($child->isRequired()) {
                    $message = sprintf('The child config "%s" under "%s" must be configured', $name$this->getPath());
                    if ($child->getInfo()) {
                        $message .= sprintf(': %s', $child->getInfo());
                    } else {
                        $message .= '.';
                    }
                    $ex = new InvalidConfigurationException($message);
                    $ex->setPath($this->getPath());

                    throw $ex;
                }

                

    abstract protected function parse();

    /** * @return void */
    public function validate()
    {
        $definition = $this->definition;
        $givenArguments = $this->arguments;

        $missingArguments = array_filter(array_keys($definition->getArguments())fn ($argument) => !\array_key_exists($argument$givenArguments) && $definition->getArgument($argument)->isRequired());

        if (\count($missingArguments) > 0) {
            throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
        }
    }

    public function isInteractive(): bool
    {
        return $this->interactive;
    }

    
public function testExpandedCheckboxesAreNeverRequired()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'multiple' => true,
            'expanded' => true,
            'required' => true,
            'choices' => $this->choices,
        ]);

        foreach ($form as $child) {
            $this->assertFalse($child->isRequired());
        }
    }

    public function testExpandedCheckboxesInhertLabelHtmlOption()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'choices' => $this->choices,
            'expanded' => true,
            'label_html' => true,
            'multiple' => true,
        ]);

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