0

I'm making sort of a slideshow for a customer

The slideshow has a field collection which has itself several fields that contain the actual content of the slides (headline, text, image, alt text, image caption)

And the slideshow is a node which uses several fields: title, teaser texts, teaser title texts, links to other pages etc.

The customer wants the slides to have individual URLs which I want to be created with pathauto module. The token which I use for a normal node (on admin/config/search/path/patterns) is like this [node:title]-[node:nid]. This works ok

But how about the token for a individual slide? If I administer the fields for my field collection (admin/structure/field-collections/field-pages/fields), there is no such default field like URL-alias.

Do I need to use the token hooks to create new tokens? But how? How can I then add them to the URL-alias? I suggest with hook_presave? Could it be done?

1 Answer 1

0

I managed it now like this in a custom module. What I do is add a new argument to the system path, so node/<nid> becomes node/<nid>/<slide_num>

Can it be made better?

/**
 * implements hook_pathauto_alias_alter()
 * alters the alias of a newly created slideshow node
 */
function coco_slideshow_pathauto_alias_alter(&$alias, &$context) {
        // print_r(array($alias, $context));
    if ($context['module'] == 'node' && $context['type'] == 'slideshow'
        && ($context['op'] == 'insert' 
            || $context['op'] == 'update')
        ) {
            $node = $context['data']['node'];

            module_load_include('inc', 'pathauto');
            module_load_include('inc', 'path');

            // get default path alias from patterns
            // this will be start of url alias
            $pattern = pathauto_pattern_load_by_entity('node', $node->type, $node->language);
            $path = pathauto_clean_alias(token_replace($pattern, array(
                'node' => $node,
            ), array(
                'sanitize' => false,
                'clear' => FALSE,
                'callback' => 'pathauto_clean_token_values',
                'language' => (object) array('language' => $node->language),
                'pathauto' => TRUE,
            )));

            $pages = field_get_items('node', $node, 'field_pages');
            if (!empty($pages)) {
                $slide_id = $pages[0]['value'];
                $slide = field_collection_item_load($slide_id);
                if (!empty($slide)) {       
                    $headline = field_get_items('field_collection_item', $slide, 'field_slide_headline');
                    $headline_text = $headline[0]['safe_value'];
                    if (!empty($headline)) {
                        $path_text = pathauto_cleanstring("{$headline_text} {$slide_id}");
                        // alias is replaced with new one
                        $alias = "{$path}-{$path_text}";
                    }
                }
            }
    }

}


/**
 * implements hook_node_insert()
 * 
 * creates aliases for each slide in slideshow node type
 */
function coco_slideshow_node_insert($node) {
    if ($node->type == 'slideshow') {
        module_load_include('inc', 'pathauto');
        module_load_include('inc', 'path');

        $title = $node->title;
        $nid = $node->nid;

        // get default path alias from patterns
        // this will be start of url alias
        $pattern = pathauto_pattern_load_by_entity('node', $node->type, $node->language);
        $path = pathauto_clean_alias(token_replace($pattern, array(
            'node' => $node,
        ), array(
            'sanitize' => TRUE,
            'clear' => TRUE,
            'callback' => 'pathauto_clean_token_values',
            'language' => (object) array('language' => $node->language),
            'pathauto' => TRUE,
        )));


        $pages = field_get_items('node', $node, 'field_pages');
        if (!empty($pages)) {
            $slides = field_collection_item_load_multiple($pages);
            if (!empty($slides)) {



                $i=0;
                foreach ($slides as $slide_id => $slide) {
                    $headline = field_get_items('field_collection_item', $slide, 'field_slide_headline');
                    if (!empty($headline)) {
                        $headline_text = $headline[0]['safe_value'];

                        // create clean string for pathauto for every single slide
                        // this will be second part of url alias
                        $path_text = pathauto_cleanstring("{$headline_text} {$slide_id}");

                        // first slide should alias to node/<nid> 
                        // not to node/<nid>/<slide_id>
                        // this is handled in hook_pathauto_alias_alter
                        if ($i > 0) {
                            $save = array(
                                'source' => "node/{$nid}/{$i}",
                                'alias' => "{$path}-{$path_text}",
                                'language' => $node->language,
                            );
                            path_save($save);
                        }
                    }
                    $i++;
                }
            }
        }
    }
}

/**
 * implements hook_node_update
 */

function coco_slideshow_node_update($node) {
    if ($node->type == 'slideshow') {
        module_load_include('inc', 'pathauto');
        module_load_include('inc', 'path');

        $title = $node->title;
        $nid = $node->nid;

        // get default path alias from patterns
        // this will be start of url alias
        $pattern = pathauto_pattern_load_by_entity('node', $node->type, $node->language);
        $path = pathauto_clean_alias(token_replace($pattern, array(
            'node' => $node,
        ), array(
            'sanitize' => TRUE,
            'clear' => TRUE,
            'callback' => 'pathauto_clean_token_values',
            'language' => (object) array('language' => $node->language),
            'pathauto' => TRUE,
        )));


        $pages = field_get_items('node', $node, 'field_pages');
        if (!empty($pages)) {
            $slides = field_collection_item_load_multiple($pages);
            if (!empty($slides)) {



                $i=0;
                foreach ($slides as $slide_id => $slide) {
                    $headline = field_get_items('field_collection_item', $slide, 'field_slide_headline');
                    if (!empty($headline)) {
                        $headline_text = $headline[0]['safe_value'];

                        // create clean string for pathauto for every single slide
                        // this will be second part of url alias
                        $path_text = pathauto_cleanstring("{$headline_text} {$slide_id}");

                        // first slide should alias to node/<nid> 
                        // not to node/<nid>/<slide_id>
                        // this is handled in hook_pathauto_alias_alter
                        if ($i == 0) {
                            $save = array(
                                'source' => "node/{$nid}",
                                'alias' => "{$path}-{$path_text}",
                                'language' => $node->language,
                            );
                            $temp = drupal_get_path_alias("node/{$nid}", $node->language);
                            path_delete(array('alias' => $temp));
                            // print '<xmp>' . print_r($save,1). '</xmp>';
                            path_save($save);
                        } else {
                            $save = array(
                                'source' => "node/{$nid}/{$i}",
                                'alias' => "{$path}-{$path_text}",
                                'language' => $node->language,
                            );
                            path_delete(array('source' => "node/{$nid}/{$id}"));
                            path_save($save);
                        }
                    }
                    $i++;
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.