When I want to generate contents for selected content types, by default, some contents are generated authored by anonymous users (not verified).
In major circumstances, we want to avoid contents generated by anonymous users(not verified).

Proposed Solution:

We can provide an option to select users so that we can choose preferred content authors for generated contents.

CommentFileSizeAuthor
#35 2929385-35.interdiff-28-35.txt7.27 KBjonathan1055
#35 2929385-35.exclude_anonymous.patch14.89 KBjonathan1055
#29 selected_8611.png202.54 KBKondratievaS
#28 2929385-28.interdiff-27-28.txt1.4 KBjonathan1055
#28 2929385-28.exclude_anonymous.patch8.68 KBjonathan1055
#27 interdiff-26-27.txt7.18 KBpiggito
#27 2929385-27.exclude_anonymous.patch8.32 KBpiggito
#26 2929385-26.exclude_anonymous.patch7.34 KBpiggito
#24 2929385-24.22-to-24.interdiff.txt7.55 KBjonathan1055
#24 2929385-24.exclude_anonymous.patch7.73 KBjonathan1055
#22 interdiff_17-22.txt579 bytesvacho
#22 exclude_anonymous-2929385-22.patch8.33 KBvacho
#19 2019-08-23_17-46.png92.23 KBdavidferlay
#19 2019-08-23_17-37.png124.34 KBdavidferlay
#17 interdiff_16-17.txt3.35 KBvacho
#17 exclude_anonymous-2929385-17.patch8.35 KBvacho
#16 Generate content drupal.png66.68 KBvacho
#16 interdiff_5-16.txt5.22 KBvacho
#16 exclude_anonymous-2929385-16.patch5 KBvacho
#11 Screenshot at feb 22 18-11-22.png266.85 KBrafuel92
#11 Screenshot at feb 22 18-07-54.png226.42 KBrafuel92
#5 exclude_anonymous-2929385-5.patch1.72 KBMomo4
#5 exclude_anonymous-2929385-5.patch1.72 KBMomo4
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Himanshu5050 created an issue. See original summary.

Himanshu5050’s picture

Issue summary: View changes
Himanshu5050’s picture

Momo4’s picture

Assigned: Unassigned » Momo4
Issue tags: -feature request +GlobalSprintWeekend2018

Adding an option to remove anonymous user from the authors.

Momo4’s picture

I added a checkbox field in the form that checks if the anonymous user has to be in the authors of generated contents; when it is checked, the db query excludes the anonymous user from selected users.

The last submitted patch, 5: exclude_anonymous-2929385-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 5: exclude_anonymous-2929385-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

rafuel92’s picture

Status: Needs work » Needs review

Setting it to needs review to run the tests

rafuel92’s picture

Assigned: Momo4 » rafuel92
Momo4’s picture

rafuel92’s picture

I've tested it on Drupal 8.5 and it works fine, you can see the attached screenshots as evidence, nodes are generated without "Anonymous User" as owner.

rafuel92’s picture

Status: Needs review » Reviewed & tested by the community
moshe weitzman’s picture

Status: Reviewed & tested by the community » Needs work

Instead of a checkbox, how about we check if anon are permitted to post comments?

davidferlay’s picture

Instead of a single checkbox, would be great to have :

  • a multiple checkbox list of available user to allow as author (like CT UI basically)
  • option in drush command --author=user1, user2
vacho’s picture

Assigned: rafuel92 » vacho
vacho’s picture

I contribute to this solution implementing the option to select users that randomly create content. Validating that users have the privilege to create the content types selected to generate.

View the image:
Example

TODO:
Needs to implement it for drush command

vacho’s picture

This patch contains #16 enhancement + drush command.

Now you can run:

$ drush --kill --types=page --authors=1,0,9 devel-generate-content 10 3

specifying the users that you can set as authors.
Where 0 is user anonymous.

vacho’s picture

Assigned: vacho » Unassigned
Status: Needs work » Needs review
davidferlay’s picture

FileSize
124.34 KB
92.23 KB

@vacho, i tested patch #17 locally :

  • Using UI : existing users are not listed in UI of admin/config/development/generate/content

generate ui broken

  • Using `drush devel-generate-content --types=basic_page --authors=2 --kill 10` : message [warning] array_rand() expects parameter 1 to be array, null given ContentDevelGenerate.php:544 is printed and generated nodes are not of given author UID

drush command ko

davidferlay’s picture

Status: Needs review » Needs work
vacho’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
vacho’s picture

Hi @davidferlay Thanks for the review of this solution.

About the points:

Using UI : existing users are not listed in UI of admin/config/development/generate/content

It is because you need to select the content type that you need to generate, so the users that have roles that let create it shows.

Using `drush devel-generate-content --types=basic_page --authors=2 --kill 10` : message [warning] array_rand() expects parameter 1 to be array, null given ContentDevelGenerate.php:544 is printed and generated nodes are not of given author UID

It is strange. Maybe versions of the base software. Please provide me drupal core version where you test, drush version too.

However, I solve some new problem that I found after review again. Maybe now you don't have any error again.

BTW
The Drush command to generate content with devel now is 'generate-content':

$ drush --kill --types=article --authors=0,1 generate-content 10

jonathan1055’s picture

Version: 8.x-2.x-dev » 8.x-3.x-dev

This needs to be done at 8.x-3.x first. The patch applies OK. I will investigate the failing tests.

jonathan1055’s picture

I like this enhancement, lots of good work here everyone :-) I've done some testing, and the first problem as shown in the test results was:

Warning: Invalid argument supplied for foreach() in Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate->settingsForm()
   (line 320 of /devel/devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php)

This is caused on the initial visit to admin/config/development/generate/content because $node_types is bound to be empty. This can be fixed by getting rid of $node_types_select and removing

    foreach ($node_types as $id => $value) {
      if ($value === 0) {
        unset($node_types_select[$id]);
      }
    }

as this is just not needed. $node_types is ready to use like it is, we can just add array_filter() to remove the 0 values in getCreators().

With this fixed, the tests still fail because the new functionality requires a second pass at the form to select the users. This is a problem, not only for the existing tests, but also because it changes the default functionality of the generation process and adds an extra step (or blockage) to the admin/developer. It can easily be fixed by having the default as "all users" like before, but we still give the admin the new abiliy to select specific users if they want to. In drush, the default of 0,1 is a restriction of previous functionality so this should be removed and we have the default as all users. This means that the tests work and pass as before, too.

The other problem is that of enforcing the assigned user to have a valid role with authority to create the content being created. This is a nice idea, but it is beyond the scope of this particular issue. It changes the flow of operations, because there are many times when a site developer does not need roles, they just want to create 20 users, then create 100 pieces of content distributed randomly among those 20 users. To enforce the role restriction means an extra step, you have to create at leat one role. And currently that cannot be done in devel_generate, so it means doing it manuallly. It's a good improvement, but needs to be discussed separately, on its own issue.

I've added an interdiff and you will see a few other changes, some just to variable names for better code understanding. The patch #22 also had a new call to $this->develGenerateContentPreNode($values) within generateBatchContent() which should not be there.

To assist in testing this change manually you will need the one-line change to ContentDevelGenerate.php in #3076613-8: Undefined index: users in $result array (it would be good to get that fix in first, so please test that and lend your weight to getting that committed). Also be aware of #3081503: Generate content in batch ignores selected node types which can make manual testing confusing. The patch on that issue would be good to get in too.

moshe weitzman’s picture

Status: Needs review » Needs work

Change status based on last post.

piggito’s picture

Rerolled last patch

piggito’s picture

As mentioned in #24 the permissions check for users goes out of scope for current issue so I removed everything related to this. Also, I implemented some missing dependency injection and one tiny code standard issue.

jonathan1055’s picture

Hi @piggito,
Thanks for re-rolling, and for removing the out-of-scope code, and adding dependency injection for user storage. This all looks quite good now.

I have made two additions:
(a) Placed the user table into a 'details' fieldset and set it to collapsed by default. If lots of users have been generated and you do not want to specify any particular ones, the long table gets means lots of scrolling down to the 'Generate' button
(b) I added the user ID into the table.

Two things I noted
(1) Is there a simple way for the user table to be sorted by clicking on the columns. This would allow easy finding of roles or user names
(2) When using in drush, if a non-existant user id is specified in the --authors parameter there is no warning message and the generated content gets assigned to the anonymous user 0. This may be OK, but wanted to state it here.

Patch and interdiff attached. [testing is currently only working at Core 8.9, see #3130041: Composer Require failure at Core 8.8 and 9.0 testing

KondratievaS’s picture

FileSize
202.54 KB

Tested patch from #28 using drush command like drush devel-generate-content --bundles=basic_page --authors=3 --kill 10 and UI and result is OK

OK

vacho’s picture

Status: Needs review » Reviewed & tested by the community

After reviewed the code is better, much more clean. and the feature works by GUI and by drush console.

I run this command and this make done:
$ drush --kill --bundles=article --authors=1 generate-content 5

andypost’s picture

+++ b/devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php
@@ -553,9 +598,17 @@ class ContentDevelGenerate extends DevelGenerateBase implements ContainerFactory
+    // Remove non-selected users. !== 0 will leave the Anonymous user in if it
+    // was selected on the form or entered in the drush parameters.
+    $authors = array_filter($authors, function ($k) {
+      return $k !== 0;
+    });
+    // If no users specified then get all.
+    if (empty($authors)) {
+      $authors = $this->userStorage->getQuery()->execute();

Why list cleaned from UID0 but "all" is not?

andypost’s picture

+++ b/devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php
@@ -553,9 +589,17 @@ class ContentDevelGenerate extends DevelGenerateBase implements ContainerFactory
+      $authors = $this->userStorage->getQuery()->execute();

@@ -671,16 +715,4 @@ class ContentDevelGenerate extends DevelGenerateBase implements ContainerFactory
-    $result = $this->database->queryRange("SELECT uid FROM {users}", 0, 50);

Returning "all" is very expensive on 500k user base

vacho’s picture

Status: Reviewed & tested by the community » Needs work
jonathan1055’s picture

Thanks @KondratievaS and @vacho for testing and reviewing.

@andypost - yes you are right. I did not consider a test or development site would have 500k users, but of course it could have. How about if the query is changed so that if no users are specified we pick a random selection with a limit of 50. Therefore, on sites with fewer than 50 users it will be all of them.

jonathan1055’s picture

Title: Option to avoid generated content authored by anonymous user » Option to set authors for generated content and avoid authored by anonymous user
Status: Needs work » Needs review
FileSize
14.89 KB
7.27 KB

I have implemented the idea in #34, limiting the user selection to 50.

Also in this patch is new test coverage for both the UI functionality and the Drush 'authors' option

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

LGTM. Thanks.

  • jonathan1055 committed 6bc207d on 8.x-3.x
    Issue #2929385 by vacho, jonathan1055, piggito, Momo4, rafuel92,...
jonathan1055’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for the review, and thank you to all who have contributed. Nice to get this in and done

  • jonathan1055 committed 0c67ddd on 8.x-3.x
    Issue #2929385 by jonathan1055: Option to set authors - correct the help...

  • jonathan1055 committed 4dd3ce0 on 8.x-3.x
    Issue #2929385 by jonathan1055: Option to set authors - improve the help...

Status: Fixed » Closed (fixed)

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