drupal 8 file upload path on node_presave hook - drupal

I need to find the file uploaded path on node_presave hook
function hooks_example_node_presave(EntityInterface $node) {
var_dump($node->field_image_upload->getValue()); exit;
}
This is what I tried. Help would be appreciated.
Thanks
Raj

I found solutions. Following will get the uploaded file url.
$node->field_image_upload->entity->url()

You need to write this
function hooks_example_node_presave(Drupal\Core\Entity\EntityInterface $entity) {
$image = $entity->field_seedit_thumbnail_image->getValue();
$fid = !empty($image[0]['target_id']) ? $image[0]['target_id'] : '';
}

Related

Wordpress Contact Form 7 check file server side

Here is my problem:
In a form made by Contact Form 7 I allow the download of a pdf, jpg, or jpeg file type.
My concern is that the extension control is not fully performed.
Indeed, if I take a "test.exe" file and rename it "text.jpg", I can submit my form and there's no errors...
Is there a solution to test this? (e.g. by the mime test)
And if so where should I implement code?
Thank you in advance for your answers and sorry for my English (I am French...)
For those who have the same question, here my solution :
In wp-includes/functions.php :
add_filter('wpcf7_validate_file*', 'cf7_custom_file_validation', 10, 2);
add_filter('wpcf7_validate_file', 'cf7_custom_file_validation', 10, 2);
function cf7_custom_file_validation ($result, $tag) {
if ($tag->name === 'file-586') {
$contentType = mime_content_type($_FILES[$tag->name]['tmp_name']);
if ($contentType !== 'image/png' && $contentType !== 'image/jpeg' && $contentType !== 'application/pdf') {
$result->invalidate($tag, 'Ce type de fichier n\'est pas supporté');
}
}
return $result;
}
I found for you some usefull answers in our forum:
PHP Uploading files - image only checking
Try this answer:
<?php
function isImage($img){
return (bool)getimagesize($img);
}
?>
Posted by Jeremy Harris.
Here is manual of getimagesize()
http://php.net/manual/pl/function.getimagesize.php
Wordpress uses the filter upload_mimes to control what MIME types are allowed site-wide. You can customize this list by adding the following to wp-includes/functions.php:
function safe_mime_types($mime_types){
unset($mime_types['exe']); //remove .exe support
return $mime_types;
}
add_filter('upload_mimes', 'safe_mime_types', 1, 1);
You can also add acceptable MIME types:
function safe_mime_types($mime_types){
$mime_types['svg'] = 'image/svg+xml'; //add .svg support
unset($mime_types['exe']);
return $mime_types;
}
add_filter('upload_mimes', 'safe_mime_types', 1, 1);

Generate Contact File (.vcf) from WordPress Post Data?

After creating new post I need to generate and store the .vcf file in storage. I don't know how I can do this after post save. Didn't find help to develop such function. Please help!
Try this :
On "save_post" hook you can write function. That will create .vcf file with the name of post_title at specified directory.
function my_project_create_vCard( $post_id ) {
$vpost = get_post($post->ID);
$filename = strtolower(str_replace(" ","-",$vpost->post_title)).".vcf";
header('Content-type: text/x-vcard; charset=utf-8');
header("Content-Disposition: attachment; filename=".$filename);
$data=null;
$data.="BEGIN:VCARD\n";
$data.="VERSION:2.1\n";
$data.="FN:".$vpost->post_title."\n"; // get post title
$data.="EMAIL:" .get_field('email',$vpost->ID)."\n"; // get acf field value
$data.="END:VCARD";
$filePath = get_template_directory()."/".$filename; // you can specify path here where you want to store file.
$file = fopen($filePath,"w");
fwrite($file,$data);
fclose($file);
}
add_action( 'save_post', 'my_project_create_vCard' );

simplexml_load_file not working to read rss feed at www.thetechjournal.com

When I try to read the rss feed at www.thetechjournal.com I get nothing. They use wordpress to generate the rss, so I assume it has something to do with that.
<?php
$url = 'http://feeds.thetechjournal.com/TheTechJournal';
$rss = simplexml_load_file($url);
print_r($rss);
?>
UPDATE: The offending XML is attached as an image.
Their feed has errors. Check it with this:
$url = 'http://feeds.thetechjournal.com/TheTechJournal';
libxml_use_internal_errors(true);
$sxe = simplexml_load_string($url);
if ($sxe === false) {
echo "Failed loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
print_r($rss);
I am from TTJ team. When I do run this Validator it shows fine, but as we have some iFrame is marks those issue. And sometime if some post have any weird char that what makes it invalid. Please point out to me any exact issue that we could look into. I do appreciate your concern.
http://validator.w3.org/feed/check.cgi?url=http%3A%2F%2Ffeeds.thetechjournal.com%2FTheTechJournal

Zend_Search_Lucene implementation with Symfony2 returning empty result

I have an implementation of Zend_Search_Lucene within a Symfony2 application. I am using Zend 1.11. The index seems to be being created, I have several files including:
_0.cfs
optimization.lock.file
read-lock-processing.lock.file
read.lock.file
segments_2
segments.gen
write.lock.file
here is the php code which I have inside a controller
$index = \Zend_Search_Lucene::create(__DIR__.'/../../../../data/index');
$doc = new \Zend_Search_Lucene_Document();
$doc->addField(\Zend_Search_Lucene_Field::unIndexed('title', 'Symfony2') );
$doc->addField(\Zend_Search_Lucene_Field::text('contents', 'cat dog') );
$index->addDocument($doc);
$index = \Zend_Search_Lucene::open(__DIR__.'/../../../../data/index');
$term = new \Zend_Search_Lucene_Index_Term("dog");
$query = new \Zend_Search_Lucene_Search_Query_Term($term);
$results = $index->find($query);
try {
$results = $index->find($query);
}
catch (\Zend_Search_Lucene_Exception $ex) {
$results = array();
var_dump($ex);
}
foreach ( $results as $result ) {
echo $result->score, ' :: ', $result->title, "n";
}
var_dump($results);
exit;
When I run the script the index files are created but only an empty array gets returned and is printed out with the last var_dump as
array(0) { }
Firstly does anyone know how I can check the index is being written correctly?
Secondly does anyone know why my query is not returning any results?
Has anyone successfully implemented Zend_Search_Lucene with Symfony2? I have tried both Lidaa Search and EWZ bundles but neither seems to work.
I worked all day yesterday trying to resolve this problem with no joy, so any help would be very greatly appreciated.
ok, so I've managed to write to the index file now by encoding as utf-8 like this
$doc->addField(\Zend_Search_Lucene_Field::text('contents', 'dog', 'utf-8') );
However, I can still not retrieve any results. I think it might have something to do with the locale settings but I have explicitly set this like so:
setlocale(LC_ALL, 'en_UK.utf-8');
ini_set('intl.default_locale', 'en-UK');
Also if I try and parse the query string as utf-8 the script hangs and timesout
$queryStr = $_GET['query'];
$query = \Zend_Search_Lucene_Search_QueryParser::parse($queryStr, 'utf-8');
Anyone know why this won't work on my Mac?
Yeehaa! I reinstalled MAMP to version 2.0.3 and Boom! it works. I have a feeling this was something to do with either the default locale or encoding but not sure what. If anyone know why version 2.0 MAMP had a bug please let us know.
Cheers
Patrick
You seem to be missing a commit.
$doc->addField(\Zend_Search_Lucene_Field::text('contents', 'cat dog') );
$index->addDocument($doc);
$index->commit(); //Try adding this line
$index = \Zend_Search_Lucene::open(__DIR__.'/../../../../data/index');

$wpdb->posts not being accepted by Wordpress Pugin

Hope you all have been doing great...
I am here today to look for an answer to my issue...
I created a plugin and activated it, it does not create a table etc just simple php script.
<?php
/*
Plugin Name: F
Plugin URI: h
Description: T
Author: D
Author URI: h
*/
$server = "localhost";
$user = "admin";
$password = "";
$db = "wordpress";
$con = mysql_connect($server,$user,$password);
if (!$con) {
die("database connection error");
} else
{
mysql_select_db($db, $con);
$results = mysql_query("SELECT ID, post_title FROM wp_posts "
. "WHERE "
. "post_status = 'publish' "
);
while($row = mysql_fetch_array($results))
{
echo $row['post_title'];
}
}
the autocomplete code is as below
$("#imageSearch").autocomplete("<?php echo bloginfo('wpurl')."/wp-content/plugins/foxycomplete/"; ?>foxycomplete.php", {
dataType: "json",
parse: function(data) {
return $.map(data, function(row) {
return {
data: row,
value: row.title,
result: $("#imageSearch").val()
}
});
}
}).result(function(e, item) {
location.href = link(item);
});
});
this is working but I am pretty sure that this is not the right way. I am not able to use the wp functions is the plugin script and also this seems unsafe and prone to hacking...
could anyone please help how I can get a php file to feed the autocomplete that can access wop functions and is safe?
Thanks a lot!
There's no such thing as a Plugin Page. The code above should probably be wrapped in a function and called from somewhere in a WordPress context, or it should be used in an action or a filter.
If you are accessing the plugin page directly rather than from within Wordpress, the problem might be that the $wpdb is not being initialized. This is done in the WordPress header, which is normally included on the page if you are inside of a Wordpress template. Try including wp-blog-header.php in your script with something like this:
include_once(‘wp-blog-header.php’);

Resources