Drupal 7: Automatically link taxonomy terms in body content

Drupal 7: Automatically link taxonomy terms in body content

http://investingtips360.com/?klaystrofobiya=%D8%A7%D9%81%D8%B6%D9%84-%D8%A8%D8%B1%D9%86%D8%A7%D9%85%D8%AC-%D8%A8%D9%8A%D8%B9-%D9%88%D8%B4%D8%B1%D8%A7%D8%A1-%D8%A7%D9%84%D8%A7%D8%B3%D9%87%D9%85&94d=44 Auto tagging of terms from a vocabulary associated with a node is reasonably simple using some existing contributed modules but if you want to dynamically insert a link to that taxonomy terms page it is not so straight forward as there are no modules that I know of that do this for you. 

الموقع هنا

http://www.homelesshounds.org.uk/?mikstyra=%D9%85%D8%A4%D8%B4%D8%B1-%D8%B3%D9%88%D9%82-%D8%A7%D9%84%D8%A7%D8%B3%D9%87%D9%85-%D8%A7%D9%84%D8%B3%D8%B9%D9%88%D8%AF%D9%8A-%D8%AA%D8%AF%D8%A7%D9%88%D9%84&ec1=d4 I wanted to add this functionality to this site to make it easier to find content about subjects like Drupal and PHP whilst reading related posts and to help with SEO and Google crawling my pages. 

انظر هنا

زيارة هذا الموقع The example below is something I have written for myself so there are no frills or admin interface as I don't need anything like that. 

يمكنك محاولة هنا

http://yorkshire-belle.co.uk/?anifer=%D8%B3%D8%B9%D8%B1-%D8%A7%D9%84%D8%B3%D9%88%D9%82-%D8%A7%D9%84%D8%B3%D8%B9%D9%88%D8%AF%D9%8A&dfe=12 In the hook_node_view function below there are 2 cases where content is linked, the first one is assuming that there is a "Tags" field (field_tags) on the node, the second assumes that there is not. It is more efficient to use the first method but the second makes life easier and saved me some time configuring a complicated rule to inspect all the fields on the node in question to apply the tags. 

http://rushmoorroyals.org.uk/?afineso=%D8%A7%D9%84%D8%AE%D9%8A%D8%A7%D8%B1%D8%A7%D8%AA-%D8%A7%D9%84%D8%AB%D9%86%D8%A7%D8%A6%D9%8A%D8%A9-%D8%A5%D8%B4%D8%A7%D8%B1%D8%A7%D8%AA-%D8%A7%D9%84%D8%B3%D9%88%D9%82&43e=b6

مراجعة إشارات التداول بالخيارات الثنائية 2017 By targeting the $node->content element the node it's self is left untouched which makes for easy editing and management while still achieving the required result. 

انظر رابط

http://sejrup-it.dk/?centosar=%D8%A8%D9%8A%D8%B9-%D8%A7%D8%B3%D9%87%D9%85-%D8%A8%D9%88%D8%A7%D9%86&e84=be Fell free to use this in any way you would like and comment if you have any questions or suggestions. 

تحقق من هنا

يزور <?php   /** * @file * Links terms from the supplied taaxonomy list in the supplied text */   // The vocab ID I want to use as tags define('MY_TAGS_VID', 1);   /** * Implements hook_node_view() * * This is where I am calling the node_tagger_tag function. */ function node_tagger_node_view($node, $view_mode, $langcode) {   // For nodes with field_tags if (!empty($node->field_tags[$langcode])) { $terms = array(); foreach ($node->field_tags[$langcode] as $key => $value) { $terms[] = taxonomy_term_load($value['tid']); }   // Tag the body node_tagger_tag($node->content['body'][0]['#markup'], $terms); }   // For the about us page (This would not be efficient if the vocab // contained lots of terms but for my purposes this is OK) if ($node->type == 'about_us') { $terms = taxonomy_get_tree(MY_TAGS_VID); // Tag the body node_tagger_tag($node->content['body'][0]['#markup'], $terms); // Tag the fields node_tagger_tag($node->content['field_column_one'][0]['#markup'], $terms); node_tagger_tag($node->content['field_column_two'][0]['#markup'], $terms); node_tagger_tag($node->content['field_column_three'][0]['#markup'], $terms); } }   /** * Links the occurances of the terms in the terms array to the taxonoy page * for that term from all occurances of that term in the supplied content. * * Removes the HTML tags and replaces them with IDs so that we dont link * text in alt tags or within code blocks * * We will then loop back through the IDs and replace them with their * corresponding html tags after the linking has been done. * * @param string $content * @param array $terms */ function node_tagger_tag(&$content, $terms) {   // declare arrays & variables $content = ' ' . $content; // First character gets dropped $ary_body = explode('<', $content); $ary_tags = array(); $no_tags_string = '';   // array of identifiers where we want to do something different // than the usual 1 tag = 1 ID processing $exceptions = array('href', 'geshifilter');   // loop itteration (amount of tags) $c = count($ary_body);   for ($i = 0; $i < $c; $i++) {   // the line from the array $line = $ary_body[$i];   // call function to return elems of line $elems = node_tagger_return_elems($line); $tag = $elems['tag']; $remainder = $elems['remainder'];     // What to do if an Anchor tag is found if (strpos($tag, 'href') !== FALSE ) {   // empty variable to hold entire anchor block $whole_anchor = ''; // need this for test later (will be apparent) $must_subtract = FALSE;   while (strpos(strtolower($whole_anchor), '</a>') === FALSE) { // the line from the array $line = $ary_body[$i];   // call function to return elems of line $elems = node_tagger_return_elems($line); $tag = $elems['tag']; $remainder = $elems['remainder'];   // whole anchor .= tag + remainder until </a> is found $whole_anchor .= $tag . $remainder;   // itterate forward one itteration $i = $i + 1;   // itteration bug fix $must_subtract = TRUE; }   // remove trailing remainder from whole_anchor $whole_anchor = mb_substr( $whole_anchor, 0, (mb_strlen($whole_anchor) - mb_strlen($remainder)) );   // subtract the last itteration + if set to do so coz outer // loop will do this for us next time round if ($must_subtract === TRUE) { $i = $i - 1; }   // build new string with no tags in it, just replacment IDs $no_tags_string .= '((##' . $i . '##))' . $remainder; // Save the TAG with the correct ID to reference the Tag ID $ary_tags[$i] = $whole_anchor; } else { // the line from the array $line = $ary_body[$i];   // call function to return elems of line $elems = node_tagger_return_elems($line); $tag = $elems['tag']; $remainder = $elems['remainder'];     // build new string with no tags in it, just replacment IDs $no_tags_string .= '((##' . $i . '##))' . $remainder; // Save the TAG with the correct ID to reference the Tag ID $ary_tags[$i] = $tag; } }   /** * for each term of the correct type do the replaces on the processed * text with NO HTML in it */ $link_vars = array( 'no_tags_string' => $no_tags_string, 'ary_tags' => $ary_tags, 'i' => $i, );   foreach ($terms as $term) { // Link the specific terms $link_vars = node_tagger_link_terms($link_vars, $term->name, $term->tid); }   $ary_tags = $link_vars['ary_tags']; $no_tags_string = $link_vars['no_tags_string'];   /** * Loop back through the HTML IDs and replace them with the HTML elements */ foreach ($ary_tags as $key => $value) { $no_tags_string = str_replace( '((##' . $key . '##))', $value, $no_tags_string ); }   $content = $no_tags_string; }   /** * Returns tag and remainder of string for hook_node_view function * * @param string $str * @return array */ function node_tagger_return_elems($str) { $tag = ''; $remainder = ''; // the HTML tag at the left of the line $tag = (mb_substr($str, 0, mb_strpos($str, '>'))); if (mb_strlen($tag) > 0) { $tag = '<' . $tag . '>'; } // the remainder of the line, after the HTML tag if ((mb_strlen($str) - mb_strpos($str, '>') - 1 ) > 0) { $remainder = ( mb_substr($str, (mb_strlen($str) - mb_strpos($str, '>') - 1) * -1) ); }   $return = array(); $return['tag'] = $tag; $return['remainder'] = $remainder;   return $return; }   /** * Glossary Type Linker Function * * Returns the origional input with the first occurance of the term linked * to the term taxonomy/term page * * @param array $link_vars * @param string $name * @param int $tid * * @return string */ function node_tagger_link_terms($link_vars, $name, $tid) { $search_terms = array( " " . $name . ".", " " . $name . ",", " " . $name . "?", " " . $name . "!", " " . $name . " ", );   foreach ($search_terms as $search_term) { if (mb_strlen(trim($search_term)) > 1) { // Repeat untill all occurances are replaced while ( mb_strpos($link_vars['no_tags_string'], $search_term) !== FALSE ) { node_tagger_link_terms_worker($link_vars, $search_term, $tid); } } }   return $link_vars; }   /** * Does the linking for the terms * * @param array $link_vars * @param string $search_term * @param int $tid */ function node_tagger_link_terms_worker(&$link_vars, $search_term, $tid) { $start_pos = strpos($link_vars['no_tags_string'], $search_term); if ($start_pos === FALSE) { $start_pos = 0; }   $search_term_length = strlen($search_term); $full_str_length = strlen($link_vars['no_tags_string']);   $str_01 = substr($link_vars['no_tags_string'], 0, $start_pos); $str_02 = substr( $link_vars['no_tags_string'], $start_pos, $search_term_length );   if ($full_str_length - $start_pos - $search_term_length > 0) { $str_03 = substr( $link_vars['no_tags_string'], ($full_str_length - $start_pos - $search_term_length) * -1 ); } else { $str_03 = ''; }   // here we have a match so build the link, add it to the tags array // replace the word with a ((##ID and increment the tags counter $the_link = l( '<strong class="normal">' . trim($str_02) . '</strong>', 'taxonomy/term/' . $tid, array('html' => TRUE) );   // add the link to the return array $link_vars['ary_tags'][$link_vars['i']] = $the_link;   // return the altered no tags string $link_vars['no_tags_string'] = $str_01 . ' ((##' . $link_vars['i'] . '##)) ' . $str_03;   // increment the tags count $link_vars['i'] ++; }

Comments

1

Comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.