Problem/Motivation

In testing #1876394: You have to specify $node->changed when comment.module is enabled. using

No title given

drush @drupal.d8 php-eval "\$node = entity_create('node', array('type' => 'page'));\$node->save();"

a node is created without the required title field.

Even calling $node->validate(); does not help preventing the node saved.

drush @drupal.d8 php-eval "\$node = entity_create('node', array('type' => 'page')); \$node->validate(); \$node->save();"

Bogus bundle name

Creating a node with non existing type 'bogus' is also possible. Calling $node->validate(); throws an exception.

drush @drupal.d8 php-eval "\$node = entity_create('node', array('type' => 'bogus'));\$node->save();"
drush @drupal.d8 php-eval "\$node = entity_create('node', array('type' => 'bogus')); \$node->validate(); \$node->save();"

gives

exception 'Drupal\Component\Plugin\Exception\PluginNotFoundException' with message 'The "entity:node:bogus" plugin does not exist.' in /Users/clemens/Sites/drupal/d8/www/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php:57 [error]

Proposed resolution

Make sure an entity is validated before saving it.
Make sure validate checks for required title field.

Remaining tasks

User interface changes

API changes

Comments

clemens.tolboom’s picture

Issue summary: View changes

Executing validation before saving fixes for invalid content type but not for the required title.

# This fails with: 
# exception 'Drupal\Component\Plugin\Exception\PluginNotFoundException' with message 'The "entity:node:bogus" plugin does not exist.' in /Users/clemens/Sites/drupal/d8/www/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php:57                                                                                                                    [error]
drush @drupal.d8 php-eval "\$node = entity_create('node', array('type' => 'bogus'));\$node->validate();\$node->save();"

# This still succeeds creating a node without title
drush @drupal.d8 php-eval "\$node = entity_create('node', array('type' => 'page'));\$node->validate();\$node->save();"
clemens.tolboom’s picture

Component: node system » update.module

Setting component to entity system.

It seems to me title field is required and it's default value is an empty string "" which is not what I read/debug from

\Drupal\Core\TypedData\Plugin\DataType\Map::isEmpty
\Drupal\Core\TypedData\Plugin\DataType\ItemList::isEmpty

esp. the latter

      // Other items are treated as empty if they have no value only.
      elseif ($item->getValue() !== NULL) {
        return FALSE;
      }
    }

but we do have an empty string "".

(next to test is bogus bundle)

clemens.tolboom’s picture

Checking with core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php we don't have covert values like NULL, 2e12,etc. NULL is an acceptable value so should be coverted.

Attached patch adds more tests but with outcomes to be discussed.

clemens.tolboom’s picture

Status: Active » Needs review
StatusFileSize
new3.39 KB

(Previous patch contained too much diff)

  1. +++ b/core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php
    @@ -55,18 +55,36 @@ public function testValidate(PrimitiveInterface $typed_data, $value, $valid) {
    +    $data[] = [$this->getMock('Drupal\Core\TypedData\Type\BooleanInterface'), 4, TRUE];
    +    $data[] = [$this->getMock('Drupal\Core\TypedData\Type\BooleanInterface'), 0.5, TRUE];
    

    Why are 4 and 0.5 not valid? Casting these (bool) should lead to TRUE

    These too currently FAIL running

    core$ vendor/phpunit/phpunit/phpunit tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest
    
  2. +++ b/core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php
    @@ -55,18 +55,36 @@ public function testValidate(PrimitiveInterface $typed_data, $value, $valid) {
    +    $data[] = [$this->getMock('Drupal\Core\TypedData\Type\StringInterface'), 1, TRUE];
    +    $data[] = [$this->getMock('Drupal\Core\TypedData\Type\StringInterface'), -1, TRUE];
    +    $data[] = [$this->getMock('Drupal\Core\TypedData\Type\StringInterface'), FALSE, TRUE];
    +    $data[] = [$this->getMock('Drupal\Core\TypedData\Type\StringInterface'), TRUE, TRUE];
         $data[] = [$this->getMock('Drupal\Core\TypedData\Type\StringInterface'), [], FALSE];
    

    Should all these pass or fail? The comment suggested they should fail. If so we must fix this.

clemens.tolboom’s picture

Component: update.module » entity system

This is still the case. Visiting the frontpage with empty title I get

Fatal error: Call to a member function displaySubmitted() on a non-object in /Users/clemens/Sites/drupal/d8/www/core/modules/node/node.module on line 604

clemens.tolboom’s picture

Issue summary: View changes

Added the $node->validate() to the examples.

mgifford’s picture

Status: Needs review » Needs work

Patch no longer applies.

cilefen’s picture

Title: Programmatically create node without title or bogus bundle name » Nodes can be created programatically without titles or with bogus bundle names

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jp.stacey’s picture

For reference, I'm still getting this bug: specifically when I run the /upgrade UI from an old Drupal 6 site. Of 16 D6 nodes with type=review, I end up with 15 fine, but the first D8 review node to be migrated (nid=1) has type=5 (i.e. the numeric string "5".)

This happens on a fresh Drupal install (although I am also using Migrate Plus and Tools, both 8.x-3.0-beta1, so perhaps they bypass something that Drupal core might prevent?)

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

matroskeen’s picture

Title: Nodes can be created programatically without titles or with bogus bundle names » Entities can be created programatically without titles or with bogus bundle names

This is applicable to other entity types as well. Example: #2994657: It is possible to create taxonomy terms for vocabularies that do not exist.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mohit_aghera’s picture

Status: Needs work » Closed (cannot reproduce)
Issue tags: +Bug Smash Initiative

I came across this issue while doing bugsmash triage.
This issue doesn't seem to be reproducible anymore.

I tried to do that in latest 11.x and got the following error.

drush php:eval "\Drupal\node\Entity\Node::create(['type' => 'basic'])->save();"
 [error]  Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null: INSERT INTO "node_field_data" ("nid", "vid", "type", "langcode", "status", "uid", "title", "created", "changed", "promote", "sticky", "default_langcode",

Please re-open again if you still notice the issue.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.