NodeType class variables should not be accessed directly. Functions should be use to access the variable. For instance use getDescription() and setDescription($description) for the protected class variable description. For a boolean variable the getter function becomes isVariableName(). In object-oriented programming this is called encapsulation.

Remaining tasks

  • Update the class variables and make them protected.
  • Create getters and setters for frequently used get and set functionality.
  • Update drupal to use the getters and setters instead of accessing variables directly.
  • There are no tests required because the added functions are only getters and setters.

For more info over what should be done see the issue summary of #2016679: Expand Entity Type interfaces to provide methods, protect the properties.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because properties should not be public, API methods should not be allowed to be sidestepped.
Issue priority Major because this meta goes across the entire system. But each child will be a normal bug.
Prioritized changes Prioritized since it is a bug and it reduces fragility.
Disruption Somewhat disruptive for core as well as contributed and custom modules:
  • BC break for anything using the public properties: code will need to convert to the methods
  • BC break for anything (mis)using properties that should not really be public: will require minor refactoring
  • BC break for alternate implementations of a given entity interface (rare/probably nonexistent): they will need to implement the new methods

But impact will be greater than the disruption, so it is allowed in the beta.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

daffie’s picture

I would like to get this fixed. So I will do a good review for posted patches.

daffie’s picture

claudiu.cristea’s picture

Status: Active » Needs review
Issue tags: -Novice
FileSize
153.2 KB

This is not really a novice issue because I needed to rename also the id and label of NodeType. There was a @todo left there and I felt that this is the right moment for that as is strong related.

Here's a first patch but due to the large number of affected files I didn't manage to test it locally. So, I'm expecting errors. Let's see.

Status: Needs review » Needs work

The last submitted patch, 3: node-type-protect-properties-2384481-3.patch, failed testing.

daffie’s picture

Issue summary: View changes
Issue tags: +Novice

@claudiu.cristea: Please do not change the class variables names. You only have to make them protected. I have looked at your patch and the functions that I would add are getName(), getDescription() and getHelp(). There is already a function id(). In case that you have no setVariableName() function you can use the build in function set('variable_name', $value) or get('variable_name') instead of getVariableName().

rpayanm’s picture

Status: Needs work » Needs review
FileSize
2.33 KB

Status: Needs review » Needs work

The last submitted patch, 6: 2384481-6.patch, failed testing.

Status: Needs work » Needs review

daffie queued 6: 2384481-6.patch for re-testing.

daffie’s picture

Status: Needs review » Needs work
+++ b/core/modules/node/src/Entity/NodeType.php
@@ -104,6 +104,71 @@ public function id() {
+  /**
+   * Sets the name of the node.
+   *
+   * @param string $name
+   *   Help information shown to the user when creating a Node of this type.
+   *
+   * @return $this
+   */
+  public function setName($name) {
+    $this->name = $name;
+    return $this;
+  }
...
+  /**
+   * Sets the description of the node.
+   *
+   * @param string $description
+   *   A brief description of this node type.
+   *
+   * @return $this
+   */
+  public function setDescription($description) {
+    $this->description = $description;
+    return $this;
+  }
...
+  /**
+   * Sets the help information of the node.
+   *
+   * @param string $help
+   *   A brief description of this node type.
+   *
+   * @return $this
+   */
+  public function setHelp($help) {
+    $this->help = $help;
+  }

These function are probably not used. Maybe only in testing. You can remove them and use set('name', $value), set('description', $value) and set('help', $value).
The other added functions need to be added to the NodeTypeInterface.

The last submitted patch, 6: 2384481-6.patch, failed testing.

areke’s picture

Assigned: Unassigned » areke
Status: Needs work » Needs review
FileSize
2.1 KB

Used daffie's comments in #9 for this patch.

Status: Needs review » Needs work

The last submitted patch, 11: protect-2384481-10.patch, failed testing.

areke’s picture

Status: Needs work » Needs review
FileSize
2.09 KB

There was a typo in the previous patch.

Status: Needs review » Needs work

The last submitted patch, 13: protect-2384481-12.patch, failed testing.

daffie’s picture

The reason that your patch failed testing is that outside the NodeType class the class variables are no longer accessible. And it will result in an error. Use the created methods to fix this. If you need the set a variable use set('variableName', $value).

+++ b/core/modules/node/src/NodeTypeInterface.php
@@ -56,6 +56,30 @@ public function displaySubmitted();
+   * Returns the help information of the node.
+   *
+   * @return string
+   *   The help text for the node.
+   */
+  public function getHelp();
+
+  /**
+   * Returns the description of the node.
+   *
+   * @return string
+   *   The description text of the node.
+   */
+  public function getDescription();
+
+  /**
+   * Returns the name of the node.
+   *
+   * @return string
+   *   The name of the node.
+   */
+  public function getName();
+

You are editing the NodeTypeInterface not the NodeInterface. So do "of the nodetype" and not "of the node".

rpayanm’s picture

Status: Needs work » Needs review
FileSize
8.62 KB

Status: Needs review » Needs work

The last submitted patch, 16: 2384481-16.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
9.36 KB

Status: Needs review » Needs work

The last submitted patch, 18: 2384481-18.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
10.33 KB

Status: Needs review » Needs work

The last submitted patch, 20: 2384481-20.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
11.35 KB

Status: Needs review » Needs work

The last submitted patch, 22: 2384481-22.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
31.36 KB

Status: Needs review » Needs work

The last submitted patch, 24: 2384481-24.patch, failed testing.

daffie’s picture

+++ b/core/modules/node/node.module
@@ -334,14 +334,14 @@ function node_add_body_field(NodeTypeInterface $type, $label = 'Body') {
-    entity_get_display('node', $type->type, 'default')
+    entity_get_display('node', $type->get('type'), 'default')

@@ -352,7 +352,7 @@ function node_add_body_field(NodeTypeInterface $type, $label = 'Body') {
-      entity_get_display('node', $type->type, 'teaser')
+      entity_get_display('node', $type->get('type'), 'teaser')

@@ -378,22 +378,22 @@ function node_entity_extra_field_info() {
-        $extra['node'][$bundle->type]['form']['langcode'] = array(
+        $extra['node'][$bundle->get('type')]['form']['langcode'] = array(

Use id() not get('type').

+++ b/core/modules/locale/src/Tests/LocaleContentTest.php
@@ -81,24 +81,24 @@ public function testContentTypeLanguageConfiguration() {
-    $this->assertRaw(t('The content type %type has been updated.', array('%type' => $type2->name)));
...
+    $this->assertRaw(t('The content type %type has been updated.', array('%type' => $type2->get('name'))));

@@ -154,12 +154,12 @@ public function testContentTypeDirLang() {
-    $this->assertRaw(t('The content type %type has been updated.', array('%type' => $type->name)));
...
+    $this->assertRaw(t('The content type %type has been updated.', array('%type' => $type->get('name'))));

+++ b/core/modules/node/src/Tests/NodeTypeTest.php
@@ -39,7 +39,7 @@ function testNodeTypeGetFunctions() {
-    $this->assertEqual($node_types['article']->name, $article->label(), 'Correct node type name has been returned.');
+    $this->assertEqual($node_types['article']->get('name'), $article->label(), 'Correct node type name has been returned.');

@@ -49,14 +49,14 @@ function testNodeTypeCreation() {
-    $web_user = $this->drupalCreateUser(array('create ' . $type->name . ' content'));
+    $web_user = $this->drupalCreateUser(array('create ' . $type->get('name') . ' content'));

@@ -137,11 +137,11 @@ function testNodeTypeDeletion() {
-    $this->drupalGet('admin/structure/types/manage/' . $type->name . '/delete');
+    $this->drupalGet('admin/structure/types/manage/' . $type->get('name') . '/delete');

@@ -149,9 +149,9 @@ function testNodeTypeDeletion() {
-    $this->drupalGet('admin/structure/types/manage/' . $type->name . '/delete');
+    $this->drupalGet('admin/structure/types/manage/' . $type->get('name') . '/delete');
...
-      t('Are you sure you want to delete the content type %type?', array('%type' => $type->name)),
+      t('Are you sure you want to delete the content type %type?', array('%type' => $type->get('name'))),

Use label() not get('name').

daffie’s picture

@rpayanm: Can you first start at changing the nodetype variables in the file NodeController.php. This will solve a lot of fails.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
32.62 KB

Let's go!

Status: Needs review » Needs work

The last submitted patch, 28: 2384481-28.patch, failed testing.

daffie’s picture

From 364 fails and 370 exceptions to 149 fails and 55 exceptions. :-)

Can you add an interdiff.txt file. It is easier to follow what you changed.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
2.16 KB
34.19 KB

Status: Needs review » Needs work

The last submitted patch, 31: 2384481-31.patch, failed testing.

rpayanm’s picture

FileSize
708 bytes
34.19 KB
rpayanm’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 33: 2384481-33.patch, failed testing.

Mile23’s picture

Issue tags: +Needs re-roll

Patch doesn't apply.

rpayanm’s picture

Status: Needs work » Needs review
Issue tags: -Needs re-roll
FileSize
2 KB
35.76 KB

Status: Needs review » Needs work

The last submitted patch, 37: 2384481-37.patch, failed testing.

rpayanm’s picture

Assigned: areke » Unassigned
Status: Needs work » Needs review
FileSize
2.6 KB
37.43 KB

damn! missing a close parenthesis ")"
:D

Status: Needs review » Needs work

The last submitted patch, 39: 2384481-39.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
2.76 KB
39.59 KB

trying again...

rpayanm’s picture

FileSize
3.99 KB
40.07 KB

Status: Needs review » Needs work

The last submitted patch, 42: 2384481-42.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
1.92 KB
41.27 KB
rpayanm’s picture

Yeah! :D

daffie’s picture

FileSize
41.3 KB
409 bytes

Super minor documentation change. See added file interdiff.txt.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The patch in comment #44 looks good to me.
I have one little documentation change. See the added interdiff.txt file.
I the patch are two new functions added: getHelp() and getDescription().
Both are also added to the NodeTypeInterface.
The problem described is the issue summary is fixed with the patch.
Despite the minor change to the patch file from me, I am giving the patch a RTBC.

@alexpott: You want to add the least number of new getter and setter functions.
This is fine by me, but this will result in some strange code constructions.
There are no setId() or setLabel() functions. What do you want to do?

+++ b/core/modules/node/src/NodeTypeForm.php
@@ -220,8 +220,8 @@ public function validate(array $form, FormStateInterface $form_state) {
-    $type->type = trim($type->id());
-    $type->name = trim($type->name);
+    $type->set('type', trim($type->id()));
+    $type->set('name', trim($type->label()));
rpayanm’s picture

FileSize
730 bytes
41.28 KB

Missing the dot.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 48: 2384481-48.patch, failed testing.

Status: Needs work » Needs review

daffie queued 48: 2384481-48.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 48: 2384481-48.patch, failed testing.

Mile23’s picture

Issue tags: +Needs reroll

Patch in #8 no longer applies.

tadityar’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
41.37 KB

Re-rolled

Status: Needs review » Needs work

The last submitted patch, 53: 2384481-53.patch, failed testing.

tadityar’s picture

Status: Needs work » Needs review
FileSize
41.27 KB

Forgot to save the file.. sorry :( Re-rolled again.

daffie’s picture

Status: Needs review » Needs work

@tadityar: Can you add an interdiff.txt file with your patch. It is for other people very convenient to see what the changes are between your patch and the previous one.

+++ b/core/modules/node/node.module
@@ -367,6 +369,27 @@ function node_entity_extra_field_info() {
+    // Add the 'language' select if Language module is enabled and the bundle
+    // has multilingual support.
+    // Visibility of the ordering of the language selector is the same as on the
+    // node/add form.
+    if ($module_language_enabled) {
+      $configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', $bundle->id());
+      if ($configuration->isLanguageAlterable()) {
+        $extra['node'][$bundle->id()]['form']['langcode'] = array(
+          'label' => t('Language'),
+          'description' => $description,
+          'weight' => 0,
+        );
+      }
+    }
+    $extra['node'][$bundle->id()]['display']['langcode'] = array(
+      'label' => t('Language'),
+      'description' => $description,
+      'weight' => 0,
+      'visible' => FALSE,
+    );
+    $extra['node'][$bundle->id()]['display']['links'] = array(

I think something went wrong here. Did you do "git reset --hard" before start working on this patch?

The last submitted patch, 55: 2384481-55.patch, failed testing.

hussainweb’s picture

Status: Needs work » Needs review
FileSize
40.51 KB

I am doing a straight reroll of #48 as there seems to be something else wrong with the other two patches. There was only one conflict in node.module.

@tadityar: Did you make any other changes? You mentioned that these patches were only rerolls.

tadityar’s picture

@daffie, hussainweb: hmm I didn't make any other change.. It had conflict in node.module and I deleted the

'<<<<<HEAD'
'=====', and 
'Applying patch from ____'

and the patch is like that. I also wonder why it has invalid syntax

Berdir’s picture

+++ b/core/modules/node/node.pages.inc
@@ -30,10 +30,10 @@ function template_preprocess_node_add_list(&$variables) {
+        'add_link' => \Drupal::l($type->label(), new Url('node.add', array('node_type' => $type->id()))),

You could replace this with $type->link().

daffie’s picture

@berdir: The idea is good, but I see a problem. In the nodetype annotation there are two links: "edit-form" and "delete-form". The same for the node annotation. There is no link to "node.add". Maybe I am missing something. If so please tell me.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

It all looks good to me.
There are 2 methods added: getHelp() and getDescription().
Both are added to the interface.
All documentation is in order.
It gets a RTBC for me.

alexpott queued 58: 2384481-58.patch for re-testing.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 58: 2384481-58.patch, failed testing.

hussainweb’s picture

Status: Needs work » Needs review
FileSize
40.53 KB

Rerolling.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Good reroll. Back to RTBC.
I was unable to create an interdiff.txt file.
Thanks hussainweb!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 65: 2384481-65.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
609 bytes
41.12 KB
a_thakur’s picture

Issue tags: +#dcdelhi
daffie’s picture

Status: Needs review » Reviewed & tested by the community

It all looks good to me.
The patch fixes the problem as described in the issue summary.
There are two new methods added to the class NodeType: getHelp() and getDescription().
All documentation is in order.
Good reroll! Back to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed ff7473e and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation to the issue summary.

  • alexpott committed ff7473e on 8.0.x
    Issue #2384481 by rpayanm, daffie, tadityar, hussainweb, areke, claudiu....
LewisNyman’s picture

Status: Fixed » Needs work

Sorry gang but it seems that something here has broken HEAD. When I got to /node/add I get the error:

Fatal error: Cannot access protected property Drupal\node\Entity\NodeType::$type in /Library/WebServer/Documents/core/themes/seven/seven.theme on line 76

webchick’s picture

Title: Make the class variables protected for NodeType » [HEAD BROKEN] Make the class variables protected for NodeType
Priority: Normal » Critical
Issue tags: +Needs tests

Confirmed. :(

cilefen’s picture

Does it follow that no test in Drupal gets 'node/add'?

larowlan’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
854 bytes
1.87 KB

failing test + fix

larowlan’s picture

Does it follow that no test in Drupal gets 'node/add'?

most likely only in stark

davidhernandez’s picture

I didn't look at it, but can confirm that #76 made the white screen of death go away.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Seems legit.

cilefen’s picture

#76 fixed it for me

The last submitted patch, 76: seven.fail_.patch, failed testing.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 76: seven.pass_.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
FileSize
3.21 KB
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

More test coverage!

daffie’s picture

Sorry about breaking head!

The patch looks good.
The breaking of head is fixed and a test is added.
It gets a RTBC from me.

webchick’s picture

Title: [HEAD BROKEN] Make the class variables protected for NodeType » Make the class variables protected for NodeType
Priority: Critical » Normal
Status: Reviewed & tested by the community » Fixed

No problem, daffie, you're a real core contributor now! :D

Committed and pushed to 8.0.x. Thanks so much for the quick work on this, larowlan!

  • webchick committed 7d01368 on 8.0.x
    Issue #2384481 follow-up by larowlan: [HEAD BROKEN] Fix references to...
larowlan’s picture

Issue tags: +CriticalADay

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

andypost’s picture

examples in should be also updated #2607294: Fix entity.api bundle examples