Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupalsearch_api_attachments_bfPluginsearch_apiprocessor;
  4.  
  5. use Drupalsearch_apiDatasourceDatasourceInterface;
  6. use Drupalsearch_apiItemItemInterface;
  7. use Drupalsearch_apiProcessorProcessorPluginBase;
  8. use Drupalsearch_apiProcessorProcessorProperty;
  9.  
  10. /**
  11. * @SearchApiProcessor(
  12. * id = "bodyfield_attachments"
  13. * label = @Translation("Body Field Attachments")
  14. * description = @Translation("Allows file links inside body fields to be indexed")
  15. * )
  16. */
  17. class BodyFieldAttachments extends ProcessorPluginBase{
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getPropertyDefinitions(DatasourceInterface $datasource = NULL){
  22. $properties = [];
  23.  
  24. return $properties;
  25. }
  26.  
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function addFieldValues(ItemInterface $item){
  31. $node = $this->getNode($item->getOriginalObject());
  32. $body = $node->body->value;
  33. $files = $this->getFilesFromBody($body);
  34.  
  35. }
  36.  
  37. protected function getFilesFromBody($text){
  38. $files = array();
  39. if (isset($text) && !empty($text)) {
  40. global $base_url;
  41. // Parse href attributes in <a> links.
  42. preg_match_all('/href=['"]([^>'"]*)['"]/', $text, $matches, PREG_SET_ORDER);
  43. foreach ($matches as $match) {
  44. // Determine if the file is local. Absolute URL could be local.
  45. // Beginning double slashes is implicit for the current page's protocol
  46. // but just apply http.
  47. if (substr($match[1], 0, 2) == '//') {
  48. $url = 'http:' . $match[1];
  49. }
  50. elseif (substr($match[1], 0, 1) == '/') {
  51. $url = $base_url . $match[1];
  52. }
  53. else {
  54. $url = $match[1];
  55. }
  56.  
  57. $parse = parse_url($url);
  58. // Get absolute URL to the file location.
  59. $path_files = file_create_url('public://');
  60. if (isset($parse['host']) and $parse['host'] == $_SERVER['HTTP_HOST']) {
  61. $uri = 'public://' . str_replace($path_files, '', $url);
  62. // Convert back things (such as %20 back to a space).
  63. $uri = urldecode($uri);
  64.  
  65. if (file_exists($uri)) {
  66. $files_load = file_load_multiple(array(), array('uri' => $uri));
  67. $file = reset($files_load);
  68. if ($file) {
  69. $files[] = (array) $file;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. return $files;
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement