Am I using getCreatedTime() the right way? - php

This is my code:
$node = Drupal::request()->attributes->get('node');
$created_time = $node->getCreatedTime();
Drush watchdog shows the following error:
Error: Call to a member function getCreatedTime() on null in Drupal...
What is the problem?

It is better to get current node like this:
$node = \Drupal::routeMatch()->getParameter('node');
But it not always return node object. For example, if you on the views page, it will returns null. So, you must check returned value:
if ($node instanceof \Drupal\node\NodeInterface) {
$created_time = $node->getCreatedTime();
}

Related

Creating default object from empty value- Code igniter

Hello I work on codeigniter and I try to eliminate warnings on my website, I block on:
$groups = array();
if ($bannished_groups) {
foreach ($bannished_groups as $k => $bannished_group) {
$groups[$k] = $this->group_model->GetGroupByID($bannished_group->groupid);
$groups[$k]->db = $bannished_group;
}
}
I have the error:
Creating default object from empty value
I tried to declare:
$groups[$k]->db = new stdClass();
but it does not work and i read the other answers but it does not help me ..
It seems like the method $this->group_model->GetGroupByID($bannished_group->groupid); don't always return an object, even if you think it does :-)
If it returns null, an empty string or false, you will get that error.
Just check it before you try and use it:
foreach ($bannished_groups as $k => $bannished_group) {
// Get the object
$obj = $this->group_model->GetGroupByID($bannished_group->groupid);
if (!is_object($obj)) {
// It's not an object, skip it and move on to the next
continue;
}
$groups[$k] = $obj;
$groups[$k]->db = $bannished_group;
}
This will make sure that your $groups-array only contains objects. If you still want to add it to the array regardless, just store the object directly in $groups[$k] instead of in the $obj-variable. The logic is the same, though.

PHP Get corresponding data, with default and error handling

I have a normal HTML menu that passes a GET statement to the url.
<li>Home</li>
Just like this, al though this is ofcourse only 1 item of the entire menu.
In a seperated file I have an function that checks if an GET or POST statement exist,
and If it exist and is not NULL then it will give the value back to where it was called.
public function getFormVariable($value){
switch (strtoupper($_SERVER['REQUEST_METHOD'])) {
case 'GET':
if (isset($_GET[$value]) && $_GET[$value] != NULL) {
return $_GET[$value];
}
else{
return false;
}
break;
case 'POST':
if (isset($POST[$value]) && $POST[$value] != NULL) {
return $POST[$value];
}
else{
return false;
}
break;
default:
return false;
}
}
And with the following code it takes the get value and finds the corrosponding class
(every class is in a seperated file, and every class is 1 link in my menu)
In this class there is just some regular functions/data that gives the content of that page.
$class = loadClass($ConfigPage->getFormVariable('Page'));
$ConfigPage->SetProperty('content', $class);
function loadClass($Page){
$class_name = 'Content' . $Page;
if(!class_exists($class_name)){
return 'Error: Content has not been found.';
}
$class = new $class_name();
return $class;
}
Explaining: The menu gives a GET value of 'Contact' which is then check by GetFormVariable() and then the corresponding class is found which gives back the content that class holds.
Now my question:
When the function LoadClass() cant find the name of the class it was given through the GET statement, it should return a error string. But this is not happening. I get a beautiful big orange error from PHP saying the following:
Fatal error: Call to a member function render() on a non-object in
E:\Program files\wamp\www\BDW\Class\Html_Page.Class.php on line 147
Line 147 is where to object is called
echo $this->content->render();
The Render function is as it says a normal return function inside the content classes.
Why do i get this error, and how do i fix it?
Second question. If there is no GET statement in the url. It gives the exact same error. which is pretty logical. But how do i make it show ContentHome when there is no GET statement in the url, and an ERROR when the value of the GET statement is incorrect.
Thank you for reading,
If there is anything unclear please tell me. English is not my native language, and after all. I am here to learn.
EDIT:
My knowledge of PHP is not great enough, so i decided when a class can not be found. it brings you back to home without any error. which i wanted in the first place.
Here is my new code which is still not working, why?
$class = loadClass($ConfigPage->getFormVariable('Page'));
$ConfigPage->SetProperty('content', $class);
function loadClass($Page){
$class_name = 'Content' . $Page;
$Default_Class = 'ContentHome';
if(!class_exists($class_name)){
//echo 'Error: Content has not been found.';
$class = new $Default_Class();
return $class;
}
else{
$class = new $class_name();
return $class;
}
$ConfigPage->Render();
}
It's happening because of this line :
if(!class_exists($class_name)){
return 'Error: Content has not been found.';
}
In the case the class doesn't exist, you're returning a string from the function, and afterwards trying to call a method render() on it. You can fix it either by changing this behaviour (don't return an error string, but use an Exception or trigger_error() ) or checking that the function loadClass() does return a valid object through is_object() for example.
Regarding your second question, you should expand your tests in loadClass() to handle the empty GET variable case, and substitute the empty string with a "Home" default value.
Update :
Example usage for is_object in your case :
$class = loadClass($ConfigPage->getFormVariable('Page'));
if(! is_object($class)) {
// if $class is not an object, then something went wrong above
throw new \Exception('Invalid data returned from loadClass()');
}
$ConfigPage->SetProperty('content', $class);

Call a member function on a non object

i want to display informations about object Activity that the function getCurrent() from the ListActivity should returns.
When i try it, it works perfectly, i have the information needed from the class, but, i have this error message on the top of the page :
Fatal error: Call to a member function getIdentifiant() on a
non-object in
/Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php
on line 34
Line 34 is here :
while($listActivities->next())
{
$current = new Activity();
$current = $listActivities->getCurrent();
echo $current->getId(); // line 34
}
And this is the getCurrent() function which return an Activity object.
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
}
I don't understand why i have this problem since it returns me the object that i want.
Please help me figuring it out. Thanks.
echo $current->getId(); // line 34
Fatal error: Call to a member function getIdentifiant() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php on line 34
WhatEVER you think happens, or whatEVER you see in your page, if the error says $current is not an object, it is not. It might not be null, but it could also be an array or anything that is not an object.
Also :
$current = new Activity();
$current = $listActivities->getCurrent();
Doesn't really makes sense to me.
Use
var_dump($listActivities->getCurrent());
To see what it exactly returns, and trust what errors say.
EDIT : And you may not even be looking at the right php script acually : The error says "getIdentifiant" while the code says "getId". Make sure you're looking at the right piece of code and refreshing the right page.
first set $this->current after $current = new Activity(); (maybe in constercture)
and you should return false if !isset($this->activities[$this->current])
also if you use $current = $listActivities->getCurrent() you lost your Activity object , it should save into another variable
here new code :
while($listActivities->next())
{
$current = new Activity();
if( $listActivities->getCurrent() )
echo $current->getId(); // line 34
}
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
return false;
}

How to change root of a node with DOMDocument methods?

How to only change root's tag name of a DOM node?
In the DOM-Document model we can not change the property documentElement of a DOMElement object, so, we need "rebuild" the node... But how to "rebuild" with childNodes property?
NOTE: I can do this by converting to string with saveXML and cuting root by regular expressions... But it is a workaround, not a DOM-solution.
Tried but not works, PHP examples
PHP example (not works, but WHY?):
Try-1
// DOMElement::documentElement can not be changed, so...
function DomElement_renameRoot1($ele,$ROOTAG='newRoot') {
if (gettype($ele)=='object' && $ele->nodeType==XML_ELEMENT_NODE) {
$doc = new DOMDocument();
$eaux = $doc->createElement($ROOTAG); // DOMElement
foreach ($ele->childNodes as $node)
if ($node->nodeType == 1) // DOMElement
$eaux->appendChild($node); // error!
elseif ($node->nodeType == 3) // DOMText
$eaux->appendChild($node); // error!
return $eaux;
} else
die("ERROR: invalid DOM object as input");
}
The appendChild($node) cause an error:
Fatal error: Uncaught exception 'DOMException'
with message 'Wrong Document Error'
Try-2
From #can suggestion (only pointing link) and my interpretation of the poor dom-domdocument-renamenode manual.
function DomElement_renameRoot2($ele,$ROOTAG='newRoot') {
$ele->ownerDocument->renameNode($ele,null,"h1");
return $ele;
}
The renameNode() method caused an error,
Warning: DOMDocument::renameNode(): Not yet implemented
Try-3
From PHP manual, comment 1.
function renameNode(DOMElement $node, $newName)
{
$newNode = $node->ownerDocument->createElement($newName);
foreach ($node->attributes as $attribute)
$newNode->setAttribute($attribute->nodeName, $attribute->nodeValue);
while ($node->firstChild)
$newNode->appendChild($node->firstChild); // changes firstChild to next!?
$node->ownerDocument->replaceChild($newNode, $node); // changes $node?
// not need return $newNode;
}
The replaceChild() method caused an error,
Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error'
As this has not been really answered yet, the error you get about not found is because of a little error in the renameNode() function you've copied.
In a somewhat related question about renaming different elements in the DOM I've seen this problem as well and used an adoption of that function in my answer that does not have this error:
/**
* Renames a node in a DOM Document.
*
* #param DOMElement $node
* #param string $name
*
* #return DOMNode
*/
function dom_rename_element(DOMElement $node, $name) {
$renamed = $node->ownerDocument->createElement($name);
foreach ($node->attributes as $attribute) {
$renamed->setAttribute($attribute->nodeName, $attribute->nodeValue);
}
while ($node->firstChild) {
$renamed->appendChild($node->firstChild);
}
return $node->parentNode->replaceChild($renamed, $node);
}
You might have spotted it in the last line of the function body: This is using ->parentNode instead of ->ownerDocument. As $node was not a child of the document, you did get the error. And it also was wrong to assume that it should be. Instead use the parent element to search for the child in there to replace it ;)
This has not been outlined in the PHP manual usernotes so far, however, if you did follow the link to the blog-post that originally suggested the renameNode() function you could find a comment below it offering this solution as well.
Anyway, my variant here uses a slightly different variable naming and is more distinct about the types. Like the example in the PHP manual it misses the variant that deals with namespace nodes. I'm not yet booked what would be best, e.g. creating an additional function dealing with it, taking over namespace from the node to rename or changing the namespace explicitly in a different function.
First, you need to understand that the DOMDocument is only the hierarchical root of the document-tree. It's name is always #document. You want to rename the root-element, which is the $document->documentElement.
If you want to copy nodes form a document to another document, you'll need to use the importNode() function: $document->importNode($nodeInAnotherDocument)
Edit:
renameNode() is not implemented yet, so you should make another root, and simply replace it with the old one. If you use DOMDocument->createElement() you don't need to use importNode() on it later.
$oldRoot = $doc->documentElement;
$newRoot = $doc->createElement('new-root');
foreach ($oldRoot->attributes as $attr) {
$newRoot->setAttribute($attr->nodeName, $attr->nodeValue);
}
while ($oldRoot->firstChild) {
$newRoot->appendChild($oldRoot->firstChild);
}
$doc->replaceChild($newRoot, $oldRoot);
This is an variation of my "Try-3" (see question), and works fine!
function xml_renameNode(DOMElement $node, $newName, $cpAttr=true) {
$newNode = $node->ownerDocument->createElement($newName);
if ($cpAttr && is_array($cpAttr)) {
foreach ($cpAttr as $k=>$v)
$newNode->setAttribute($k, $v);
} elseif ($cpAttr)
foreach ($node->attributes as $attribute)
$newNode->setAttribute($attribute->nodeName, $attribute->nodeValue);
while ($node->firstChild)
$newNode->appendChild($node->firstChild);
return $newNode;
}    
Of course, if you show how to use DOMDocument::renameNode (without errors!), the bounty goes for you!
ISTM in your approach you attempt to import nodes from another DOMDocument, so you need to use the importNode() method:
$d = new DOMDocument();
/* Make a `foo` element the root element of $d */
$root = $d->createElement("foo");
$d->appendChild($root);
/* Append a `bar` element as the child element of the root of $d */
$child = $d->createElement("bar");
$root->appendChild($child);
/* New document */
$d2 = new DOMDocument();
/* Make a `baz` element the root element of $d2 */
$root2 = $d2->createElement("baz");
$d2->appendChild($root2);
/*
* Import a clone of $child (from $d) into $d2,
* with its child nodes imported recursively
*/
$child2 = $d2->importNode($child, true);
/* Add the clone as the child node of the root of $d2 */
$root2->appendChild($child2);
However, it is far easier to append the child nodes to a new parent element (thereby moving them), and replace the old root with that parent element:
$d = new DOMDocument();
/* Make a `foo` element the root element of $d */
$root = $d->createElement("foo");
$d->appendChild($root);
/* Append a `bar` element as the child element of the root of $d */
$child = $d->createElement("bar");
$root->appendChild($child);
/* <?xml version="1.0"?>
<foo><bar/></foo> */
echo $d->saveXML();
$root2 = $d->createElement("baz");
/* Make the `bar` element the child element of `baz` */
$root2->appendChild($child);
/* Replace `foo` with `baz` */
$d->replaceChild($root2, $root);
/* <?xml version="1.0"?>
<baz><bar/></baz> */
echo $d->saveXML();
I hope I am not missing anything but I happened to have the similar problem and was able to solve it by using use DomDocument::replaceChild(...).
/* #var $doc DOMDocument */
$doc = DOMImplementation::createDocument(NULL, 'oldRoot');
/* #var $newRoot DomElement */
$newRoot = $doc->createElement('newRoot');
/* all the code to create the elements under $newRoot */
$doc->replaceChild($newRoot, $doc->documentElement);
$doc->documentElement->isSameNode($newRoot) === true;
What threw me off initially was that $doc->documentElement was readonly, but the above worked and seems to be much simpler solution IF the $newRoot was created with the same DomDocument, otherwise you'll need do the importNode solution as described above. From your question is appears that $newRoot could be created from the same $doc.
Let us know if this worked out for you. Cheers.
EDIT: Noticed in version 20031129 that the DomDocument::$formatOutput, if set, does not format $newRoot output when you finally call $doc->saveXML()

PHP: Create new node inside parent

I have a root XML node called and I am trying to add a new child called to this but I am getting errors. Inside there is also children. Here is my code:
$xml = new DomDocument();
$xml->load(X_ASSETS);
$xml->formatOutput = true;
$new_id = $this->getNewAssetId();
// Root
$xpath = new DOMXPath($xml);
$assets = $xpath->query('assets');
$xml_assets = $assets->item(0);
$xml_root = $xml->createElement('asset');
// Asset Name
$xml_name = $xml->createElement('name');
$xml_name->nodeValue = $clean_name;
$xml_root->appendChild($xml_name);
// Asset URL
$xml_url = $xml->createElement('url');
$xml_url->nodeValue = '/'.$name;
$xml_root->appendChild($xml_url);
// Asset ID
$xml_id = $xml->createElement('id');
$xml_id->nodeValue = $new_id;
$xml_root->appendChild($xml_id);
// Create our document and save
$xml_assets->appendChild($xml_root);
$xml->save(X_ASSETS);
I get the following error when running this:
Fatal error: Call to a member function appendChild() on a non-object in /home/websites/zed_x/core/includes/x.inc on line 88
Does anyone know what I am doing wrong here?
Somehow your $xml_assets is not an object, and therefore you cannot call the function:
$xml_assets->appendChild($xml_root);
Are you certain positive that the following command returns an object?
$xml_assets = $assets->item(0);
Test it:
if(is_object($xml_assets))
{
echo "Object Here!";
}
This might be a good way to structure your code so you can catch errors
// .... stuff .....
$xml_assets = $assets->item(0);
// ... more stuff ....
// Check for Object
if(!is_object($xml_assests))
{
die("No Object Created!");
}
$xml_assets->appendChild($xml_root);
$xml->save(X_ASSETS);
// .... more stuff .....

Categories