Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupalsearch_api_attachments_bfPluginsearch_apiprocessor;
- use Drupalsearch_apiDatasourceDatasourceInterface;
- use Drupalsearch_apiItemItemInterface;
- use Drupalsearch_apiProcessorProcessorPluginBase;
- use Drupalsearch_apiProcessorProcessorProperty;
- /**
- * @SearchApiProcessor(
- * id = "bodyfield_attachments"
- * label = @Translation("Body Field Attachments")
- * description = @Translation("Allows file links inside body fields to be indexed")
- * )
- */
- class BodyFieldAttachments extends ProcessorPluginBase{
- /**
- * {@inheritdoc}
- */
- public function getPropertyDefinitions(DatasourceInterface $datasource = NULL){
- $properties = [];
- return $properties;
- }
- /**
- * {@inheritdoc}
- */
- public function addFieldValues(ItemInterface $item){
- $node = $this->getNode($item->getOriginalObject());
- $body = $node->body->value;
- $files = $this->getFilesFromBody($body);
- }
- protected function getFilesFromBody($text){
- $files = array();
- if (isset($text) && !empty($text)) {
- global $base_url;
- // Parse href attributes in <a> links.
- preg_match_all('/href=['"]([^>'"]*)['"]/', $text, $matches, PREG_SET_ORDER);
- foreach ($matches as $match) {
- // Determine if the file is local. Absolute URL could be local.
- // Beginning double slashes is implicit for the current page's protocol
- // but just apply http.
- if (substr($match[1], 0, 2) == '//') {
- $url = 'http:' . $match[1];
- }
- elseif (substr($match[1], 0, 1) == '/') {
- $url = $base_url . $match[1];
- }
- else {
- $url = $match[1];
- }
- $parse = parse_url($url);
- // Get absolute URL to the file location.
- $path_files = file_create_url('public://');
- if (isset($parse['host']) and $parse['host'] == $_SERVER['HTTP_HOST']) {
- $uri = 'public://' . str_replace($path_files, '', $url);
- // Convert back things (such as %20 back to a space).
- $uri = urldecode($uri);
- if (file_exists($uri)) {
- $files_load = file_load_multiple(array(), array('uri' => $uri));
- $file = reset($files_load);
- if ($file) {
- $files[] = (array) $file;
- }
- }
- }
- }
- }
- return $files;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement