First ECA approach, "user-loggedin"->"has created own node" condition.

Created on 1 February 2024, 11 months ago
Updated 6 March 2024, 10 months ago

Problem/Motivation

First of all, thank you for this module, which seems to replicate the usability of the old Rules version 7.
I am trying to implement with ECA this action:
Each user, registered as a 'company' or 'professional' can create their own respective 'company' or 'professional' node.

I would like to make it so that only if the user "has not yet created his own node" will a message appear alerting him to the possibility of doing so at (after creating one he will have no way to create another *).

For the moment I have managed to create a working "user has logged in->role is...->show message to user" event, but I can't figure out how to add the "has created own content" condition.

* That might be fine, although a crafty user might imagine that typing the full path 'https://sitename.com/node/add/contentype' might, having initially been given permission to create that type of node, create another one.

Can you help me understand if this is feasible with ECA?
Thanks to all

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Fixed

Version

1.1

Component

Miscellaneous

Created by

🇮🇹Italy senzaesclusiva

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @senzaesclusiva
  • 🇩🇪Germany jurgenhaas Gottmadingen

    It depends, where the information is being stored and how one could find out, if a certain entity had already been created or not.

    Let's assume, the user entity had an entity reference field to that other entity, you could use a condition to check if that field in the user entity is empty or not. If it's empty, the user hasn't created one and you can do your magic then.

  • 🇮🇹Italy senzaesclusiva

    Thank you for your response
    The data is all stored in the default DB. Indeed, an empty field referring to the user type should indicate that I have not yet created any of its nodes.
    In Rules D7 I used to realize this condition thanks to a PHP plugin that allowed me to run a query on the DB

    global $user;
    $result = db_query("SELECT nid FROM node JOIN users ON node.uid = users.uid WHERE node.status = :status AND users.uid = :uid AND node.type = :type", array(":status"=> 1, ":uid" => $user->uid, ":type" => 'professional'))->fetchField();
    if ($result >= 1){ 
    return FALSE;
    }
    else{
    return TRUE;
    }
    

    If the result returned TRUE the action was executed.

    I will try what you suggested
    Thanks again

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Well, writing PHP in the front end is the most UNSAFE feature one can think of and ECA will never add support for anything like that.

    However, with ECA you can run a view, so you can build the SQL query that way. Then you can check if that view returned 1 or no record. That's pretty much the same.

    Still, if I were in your shoes, I'd go for a (invisible) entity reference field from the user entity to the created node. That's much more performant and easier to work with. It probably will provide more benefits down the road as well.

  • 🇮🇹Italy senzaesclusiva

    I know, but "back then" it was used :-)
    Great if ECA can use views!
    Thanks also for the suggestion of the hidden field...hadn't thought of that, sic!
    Thanks again

  • Status changed to Fixed 10 months ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Assuming this is fixed, please feel free to re-open if not.

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

Production build 0.71.5 2024