1

How can I create a custom Rule-Action which will successfully save a value as a replacement pattern for use in the other actions?

I got some very good help here on retrieving Product-Display information from a Product-Order.

As I said, the linked answer helped a great deal but the returned path data for the Product-Display comes back in the http://www.mysite/node/77 format. However, I really just need the numeric value only so I can load the node by performing a Fetch entity by id action supplying the numeric value and publishing the Product-Display node etc.

So, I implemented a custom action which will take the Product-Display URL(node/77) and return 77. I copied the Fetch entity by id code and modified it so my returned numeric value can be saved and used in other Actions. The code is below:

function my_custom_action_info(){
   $actions['publish_product_display_node'] = array(
      'label' => t('Fetch product-display id'),
      'parameter' => array(
        'type' => array(
          'type' => 'uri',
          'label' => t('My Action'),
          'options list' => 'rules_entity_action_type_options2',
          'description' => t('Specifies the product-display url.'),
        ),
      ),
      'provides' => array(
        'entity_fetched' => array('type' => 'integer', 'label' => t('Fetched entity')),
      ),
      'group' => t('Entities'),
      'access callback' => 'rules_entity_action_access',
    );

    return $actions;
}

function publish_product_display_node($path = null){
    $parts = explode('node/', $path);
    return $parts[1];
}

function rules_entity_action_type_options2($element, $name = NULL) {
  // We allow calling this function with just the element name too. That way
  // we ease manual re-use.
  $name = is_object($element) ? $element->getElementName() : $element;
  return ($name == 'entity_create') ? rules_entity_type_options2('create') : rules_entity_type_options2();
}

function rules_entity_type_options2($key = NULL) {
  $info = entity_get_info();
  $types = array();
  foreach ($info as $type => $entity_info) {
    if (empty($entity_info['configuration']) && empty($entity_info['exportable'])) {
      if (!isset($key) || entity_type_supports($type, $key)) {
        $types[$type] = $entity_info['label'];
      }
    }
  }
  return $types;
}

function rules_action_entity_createfetch_access2(RulesAbstractPlugin $element) {
  $op = $element->getElementName() == 'entity_create' ? 'create' : 'view';
  return entity_access($op, $element->settings['type']);
}

As I said I copied the modified code so I don't claim to thoroughly understand all the functions aside from publish_product_display_node.

My code modifications work as far as setting the Product-Display URL token as the argument and also setting an entity variable label(Display NID) and value(display_nid). The problem is when I check display_nid in newly created actions, the value is empty.

I need help figuring out the how to successfully save my entity value so I can use it in following Actions.

Community
  • 1
  • 1
sisko
  • 9,604
  • 20
  • 67
  • 139

1 Answers1

0

in the function publish_product_display_node, can you verify that you don't need to be returning $parts[0], instead of $[parts[1]?

It's just that Drupal paths are frequently in the form 'node/7' or 'taxonomy/term/6', and if you explode with 'node/' as the separator, you'd only have a single value which would start at index 0 for nodes...

So, just wondering if that would solve your issue...

Boriana Ditcheva
  • 1,995
  • 15
  • 26
  • Hello again Boriana. I see your point. $parts[1] was a left over from previous code and you are right it should be index 0 instead. I will look into this as soon as I am able and get back to you – sisko Jul 31 '13 at 09:36
  • Hi Boriana, I had a look at my code and updated my question with the full URL format. The full URL is of the format: http://www.mysite/node/77. So, $parts[1] is actually correct. Also, even if I returned a static number from the code, I still don't get the number in my Rule-Action – sisko Jul 31 '13 at 22:13