Yii2 / nav bar / Hiding property of non-object - object

In old version of Yii this part of navbar code is working while user is not logged in.
array('label'=>Yii::t('ge',Yii::app()->user->name), 'url'=>array('/site/index'), 'visible'=>!Yii::app()->user->isGuest),
In Yii2
['label' => Yii::$app->user->identity->username, 'url' => ['site/index'], 'visible'=>!Yii::$app->user->isGuest],
Throws "Trying to get property of non-object" meaning that object Yii::$app->user->identity->username does not exists.
How can I fix this problem ? And why old version is working OK ?

You should simply use :
'label' => Yii::$app->user->isGuest ? 'Guest' : Yii::$app->user->identity->username
Or extends \yii\web\User to handle this.
In Yii2 :
#property IdentityInterface|null $identity The identity object associated with the currently logged-in user. null is returned if the user is not logged in (not authenticated).
In Yii1, CWebUser provides default name. Feel free to ask this feature for Yii2 here : https://github.com/yiisoft/yii2/issues
EDIT : if you just want to hide this menu item for guests, add this to your item :
'visible' => !Yii::$app->user->isGuest,

Related

Acumatica Mobile: Unable to cast object of type 'System.Int32' to type 'System.String'

I'm getting this cast error after adding AR301000 to the mobile app. Version 19.103.0030 of Acumatica. I'm getting this after clicking on a Invoice on the list screen to get into the details screen. One of the Invoices works fine but, the rest are getting the cast error. How can I debug this?
Here's the code that I added:
add screen AR301000 {
add container "InvoiceSummary"{
#add field "Type"
#add field "ReferenceNbr"
#add field "Status"
#add field "Date"
#add field "Customer"
#add field "Location"
#add field "CustomerOrder"
add field "Description"
#add field "SalesApproval"
#add field "FinalApproval"
#add field "DetailTotal"
add recordAction "Save" {
behavior = Save
}
add recordAction "Cancel" {
behavior = Cancel
}
}
}
This code is working without any issue on my system.
You can debug the code as you debug any other customization's code.
Also, you can check the Stack Trace and SQL Requests in the Request Profiler page.
Go to Request Profiler (SM205070), check all the check-boxes "Log ..." and click Refresh Results.

Drupal8 partial keyword searching

I integrated Drupal 8 autocomplete module in my project. Its working fine. At the end search suggestion liss, there is a link for view all results. When I click on the link, it goes to localhost/sampleapp/search/node?keys=test. I got all the results for the keyword "test". But the problem is when the keyword is tes instead of test, I got no results. For example localhost /sampleapp/search/node?keys=tes
Acquia Search supports Solr N-Gram (partial-word search) text fields for Drupal 8 websites using the Search API module.
In the admin menu, go to Configuration > Search and metadata > Search API.
Identify the search index that you want to modify, and then click it.
Click the Fields tab.
For each field that you want to configure for partial-word searching, click Type, and then click Fulltext NGram.
Click Save.
https://docs.acquia.com/acquia-search/relevant-results/partial/
You can alter the queries provided by default.
A similar action you can look into this as reference:
<?php
// Add alter hook to be able to change suggestions in your own module.
$alter_data = array(
'query' => $query,
'response' => $response,
'incomplete_key' => $incomplete_key,
'search' => $search,
);
drupal_alter('search_api_solr_autocomplete_suggestions', $alter_data, $suggestions);
?>
Hope this help.
Thanks.

Orchard Fields Validation

In Orchard 1.4.2, when fields like input field are marked as required, validation pop up appears. How is the validation happening without re-directing the user to another view?
To explain in details, If I have a form that I have attached to a page, the form field validation displays an error message ( may be it is through AJAX or javascript), but there is no re-direction to the 'form' view - it stays on that page view.
I need to add validation to another module without it re-directing to another view.
Any idea?
You can find this bit of code in the InputFieldDriver class...
if (settings.Required && string.IsNullOrWhiteSpace(field.Value)) {
updater.AddModelError(GetPrefix(field, part), T("The field {0} is mandatory.", T(field.DisplayName)));
}
Adding a model error will send the user right back to the same page.
FYI, the fields module is a sub repository located here: https://orchardfields.codeplex.com/
The class I'm talking about is right here: https://orchardfields.codeplex.com/SourceControl/changeset/view/4d125be1a6b3#Drivers%2fInputFieldDriver.cs

Kohana 2.3/3 routing?

I've been thinking how to make something like this in Kohana:
domain.com/variable
the "variable" is the identifier for a user.
So, is this possible?
http://domain.com/username/controller/action
If yes, can you point me to the right direction, please?
Thanks.
What happens if you have a page named "contact" and a user signs up with "contact" as a username. Which page will be displayed?
Here is an example I threw together for you.
// Pages that aren't users.
Route::set('static', '<action>', array('action' => 'sitemap|faq|terms|privacy|credits'))
->defaults(array(
'controller' => 'static'
));
// User routing
Route::set('user', '<username>(/<controller>(/<action>))')
->defaults(array(
'controller' => 'user',
'action' => 'index'
));
So when this URL is called
http://example.com/sitemap
the first route is used and when
http://example.com/arnold
is called your user class and index method would be called. You can access the username variable with:
$this->request->param('username');
If you have any more questions, feel free to ask.

Editing my own user page in Grails

I'm building a web application with Grails, using the Acegi/Spring Security plugin.
I want to only show the 'Edit' link if the page is showing the details of the currently logged in user.
For example, the logged in user with id = 44 is viewing the page 'localhost:8080/app/user/show/44'
I've tried the following but it's not working. Any ideas on how to make this work, or is there some really simple way I've missed?
<g:isLoggedIn>
<g:if test="${person.id == loggedInUserInfo(field='id')}">
<g:link controller="user" action="edit" id="${person.id}">Edit</g:link>
</g:if>
</g:isLoggedIn>
i don't know if it is just a typo in this question but loggedInUserInfo should be called with a map.
you make an assignment in the method call, which results in giving just the value 'id' to loggedInUserInfo
instead of field='id' it should say field : 'id'
<g:isLoggedIn>
<g:if test="${person.id == loggedInUserInfo(field : 'id')}">
<g:link controller="user" action="edit" id="${person.id}">Edit</g:link>
</g:if>
</g:isLoggedIn>

Resources