2

How do you alter the URL generated by CKEditor Link, for a specific content type.

For example I have book_section content type. If a content editor links to a node with type of book_section I want to step in and link to a fragment/anchor within the parent node instead. The convention of my anchor ids are <a id="/node-alias" />

As per documentation provided by the CKEditor Link module author I've used hook_ckeditor_link_types and hook_ckeditor_link_TYPE_url in a custom module to make the following modifications.

/**
 * Implements hook_ckeditor_link_types
 */
function ajf_ckeditor_link_types() {
    return array('book_section');
}

/**
 * Implements hook_ckeditor_link_TYPE_url
 *
 */
function ajf_ckeditor_link_book_section_url( $path, $langcode ) {
    if (!preg_match('`^node/(\d+)$`', $path, $matches)) {
        return;
    }

    $nid = $matches[1];
    $node = node_load( $nid );

    if ( 'book_section' != $node->type ) {
        return;
    }

    $parent = menu_link_load( $node->book['plid'] );
    $options = array( 'fragment' => '/'. $node->path['alias'] );
    $url = ckeditor_link_url( $parent['link_path'], $langcode, $options );
    return $url;
}

When viewing a page with links to book_section nodes I'm getting an empty response from the server. I'm thinking the repeated node_load calls is exceeding the servers resources.

Appreciate advice you may have.

0

Your Answer

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

Browse other questions tagged or ask your own question.