Display html element based on product attribute set? - attributes

I wish to display a static block on the product page if the product belongs to a certain Attribute Set.
The idea is to have a block show on product pages if the page has an attribute set of "Rc" else do not show block. I have a custom theme I made and already have a block made and displayed on ALL product pages. I would only need the block showing on product pages with an attribute set of "Rc". I am unaware of the folder structure and/or if the following code is applicable to magento 2.3. Where do i Copy template file to and from... basically the whole nine yards of how to implement the setting and code.
The code I found is as follows (with my comments):
"Add this method to the Product View Block" From what i'm reading the view block is no more its now called the catalog_product_view.xml of which the folder structure is
app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_product_view.xml
public function checkAttributeSet($product = null, $attributeSetName = null)
{
if(is_null($product) || is_null($attributeSetName))
return false;
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
if($attributeSetModel->getAttributeSetName() == $attributeSetName) {
return true;
} else {
return false;
}
}
"Then in app/design/frontend/package/theme/template/catalog/product/view.phtml:" is the view/phtml file no longer used and what is this the correct folder structure.
if($this->checkAttributeSet($_product, 'Rc')):
echo $this->getLayout()->createBlock('cms/block')->setBlockId('Rc')->toHtml();
elseif($this->checkAttributeSet($_product, 'ORC')):
echo $this->getLayout()->createBlock('cms/block')->setBlockId('ORC')->toHtml();
endif;
What i have set up is (default.xml)
<referenceBlock name="product.info.main">
<block class="Magento\Catalog\Block\Product\View" name="product-rc" template="Magento_Theme::product-rc.phtml" after="product.info.price">
</block>
</referenceBlock> -->
product-rc.phtml is working and showing in all products.
(test block) text string is in the phtml file.

I got it. The below code worked for me.
Add code to
module-catalog/view/frontend/templates/product/view
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());
$attributeSetRepository = $attributeSet->get($product->getAttributeSetId());
$attribute_set_name = $attributeSetRepository->getAttributeSetName();
//$attribute_set_name_arr[] = $attribute_set_name;
//echo '<pre>'; print_r($attribute_set_name);
if( !empty($attribute_set_name) && $attribute_set_name == 'Rc' ) {
// echo $this->getLayout()
->createBlock('Magento\Cms\Block\Block')
->setBlockId('rcitems')
->toHtml();
}
?>
setBlockId = The Identifier of the block in admin.
Rc = is the attribute set
no need to add to default.xml

Related

Gravity Forms How to set the value of a simple text field at page render

I want to set the value of a simple text field based on the state of another field in a previous page of a form. I am selecting a hosting provider of 'aws' and later in the form, I am asking for the user name.
For aws hosting, I want to force ec2-user to be shown in the text box, so I am using the filter of
add_filter( 'gform_pre_render_4', 'populate_deployment_user' );
function populate_deployment_user( $form )
{
$hosting_provider = rgpost('input_23');
if (strcmp($hosting_provider, "aws") == 0)
{
foreach ( $form['fields'] as &$field ) {
if ( $field->id == 42) {
$field->text = "ec2-user";
}
}
}
}
but the $field->text is not correct. I can't use the gform_field_value_$field_name as that's only called at the start of the form, and not after my other field 23 has been selected.
I'm a newbie in forms, JS and PHP, so floundering somewhat, although I've tried for a couple of days to get a solution.
Solution is to set
$field->defaultValue = "ec2-user";

Drupal Page Title Block

Today I've try to add image_field in "Page Title" block.
I don't know if it's possible ?
If it isn't possible, it is possible to add field_image in twig template ?
Tnks.
The below may help you. First im adding a theme suggestion for page_title, so that i can control the place where i have to over ride the template. I am adding node_type - as suffix to the template.
Then i am adding the preprocess function- make sure you replace the signature with the type you want "news" at the end should be replaced with your content type from the first function.
In the second function i am fetching a field called subtitle, and adding the value to variables so that it is available in the template.
function mytheme_theme_suggestions_alter(array &$suggestions, array &$variables, $hook) {
if($hook === 'page_title') {
if($node = \Drupal::routeMatch()->getParameter('node')){
$node_type = $node->getType();
// suggestion must use _ and __ as per convention.
$suggestions[] = 'page_title__'.$node_type;
}
}
}
function mytheme_preprocess_page_title__news(&$variables) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
try {
if ($node->get('field_subtitle') && !$node->get('field_subtitle')->isEmpty()) {
$variables['sub_title'] =
$node->get('field_subtitle')
->get(0)
->get('value')
->getValue();
}
}catch(Exception $e){
\Drupal::logger('mytheme')->error($e);
}
}
}
Now in the template you will sub_title available in the template file, in my case it was page-title--news.html.twig - copied from page-title.html.twig and added respective variable.

How To find Cross References(Internal Links) In Pdf File Using ItextSharp Lib

Hi I am using ItextSharp For searching Cross References(Internal Links) In pdf file. I already done with External Links.
Please Post If u have any solutions.
//Get the current page
PdfDictionary PageDictionary = R.GetPageN(page);
//Get all of the annotations for the current page
PdfArray Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);
//Make sure we have something
if ((Annots == null) || (Annots.Length == 0))
// return null;
{
Console.WriteLine("nothing");
}
//Loop through each annotation
if (Annots != null)
{
foreach (PdfObject A in Annots.ArrayList)
{
//Convert the itext-specific object as a generic PDF object
PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(A);
//Make sure this annotation has a link
if (!AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK))
continue;
//Make sure this annotation has an ACTION
if (AnnotationDictionary.Get(PdfName.A) == null)
continue;
//Get the ACTION for the current annotation
PdfDictionary AnnotationAction = AnnotationDictionary.GetAsDict(PdfName.A);
// PdfDictionary AnnotationAction = (PdfDictionary)AnnotationDictionary.Get(PdfName.A);
//Test if it is a URI action (There are tons of other types of actions, some of which might mimic URI, such as JavaScript, but those need to be handled seperately)
if (AnnotationAction.Get(PdfName.S).Equals(PdfName.URI))
{
PdfString Destination = AnnotationAction.GetAsString(PdfName.URI);
string url1 = Destination.ToString();
}
}
}
You've already done most of the work. Please take a look at the following screen shot:
You see the /Annots array of a page. You are already parsing that array in your code and you skip all annotations that aren't of the /Subtype /Link or don't have an /A key, which is excellent.
Currently you're only looking for values of /S that are of type /URI. You say you're already done with external links, but that's not true: you should also lok for entries where /S is /GoToR (remote goto). If you want internal links, you need to look for /S values equal to /GoTo, /GoToE, and (in the future) /GoToDp. Maybe you also want to remove the /JavaScript actions, because they can also be used to jump to a specific page.
Please download The ABC of PDF and take a look at table 3.11 for more info. (The book is available for free.)

On Orchard CMS, how can I display a message when the query returns no records for a projection

Using the admin panel on Orchard CMS I've created the following:
A content type called CalendarEvent, it contains several fields including the EventDate;
A query that has 2 filters, one by the content type (= CalendarEvent) and another one based on the date of the event. The Display Mode on the Layout is set to Properties;
A projection to display the query when a menu item is clicked.
The problem is that based on the EventDate we only display upcoming events, not the ones in the past. If for some reason there are no events to be displayed, all the user gets is an empty page with no information whatsoever.
My question is, how can I modify my query or projection in order to display something like: "There are no current events scheduled"?
I know the Properties on the Query's Layout allow me to specify a "No Result", but this implies that a record is present and that the actual property is empty. However, in my example, no record is present.
Thank you all in advance.
Rafael
By the way, I am using the latest Orchard version 1.6.
What I have done is to create a shape and use it as a view in my query. The shape will then have an if statement to check if the query return gives any results.
Example:
#using Orchard.ContentManagement
#using Orchard.Utility.Extensions
#{
var dealsTerms = ((IEnumerable<ContentItem>)Model.ContentItems).ToList();
}
#if (dealsTerms.Any() )
{
<div>
#foreach (var dealTerm in dealsTerms)
{
var contentManager = dealTerm.ContentManager;
<div>
#Display(contentManager.BuildDisplay(dealTerm, "Summary"))
</div>
}
</div>
}
else
{
<p>No deals found</p>
}
I used this article as reference:
http://www.ideliverable.com/blog/ways-to-render-lists-of-things
Good luck
If your projections are Layouts elements, you can create a Projection.cshtml file in your theme's Views/Elements folder with the following:
#{
var list = Model.List;
var pager = Model.Pager;
if (list != null)
{
#Display(list)
if (list.Items.Count == 0)
{
<div>There are currently no items.</div>
}
}
if (pager != null)
{
#Display(pager)
}
}
This is a copy of the default template with the if (list.Items.Count == 0) section added. Edit as needed.

How does one get Drupal's current view/page identifier?

What I am looking for is a page_id/view_id that I can use to identify and style specific pages. I would use the title or the url, but there is a chance that it could change if the a higher-up decides that the page should no longer be called Golf, but rather Tee-Time because he likes it better.
Presumably this identifier would not change if the current page were to be a paged view (page 1,2,3,4...).
One way of solving this is the following. It's depending on the url, so if it changes, so does the class-name.
In my themes template.php I implemented hook_preprocess_page:
function mytheme_preprocess_page(&$vars, $hook) {
$body_classes = array();
$body_classes[] = 'page-' . _get_page_name($_SERVER['REQUEST_URI']);
$vars['body_classes'] = implode(' ', $body_classes);
}
function _get_page_name($request_uri) {
static $numeric_subsection = array(
'/node/' => 'node',
);
$preAlias = $request_uri;
$alias = substr(strrchr($preAlias, "/"), 1);
if (strpos($alias, '?') > -1) {
$alias = substr($alias, 0, strpos($alias, '?'));
}
$page_name = $alias;
if (empty($alias)) {
$page_name = 'start';
}
else if (is_numeric($alias)) {
foreach ($numeric_subsection as $section => $pn) {
if (strpos($preAlias, $section) > -1) {
$page_name = $pn;
}
}
}
return $page_name;
}
Then in the main page-template:
<body class="<?php print $body_classes; ?>">
This isn't a generic solution. So you'll probably have to customize this for your specific needs. It will for example need som tweaking to play nicely with path auto.
This depends a little on how your site is put together (panel pages, view pages, "normal" pages). Essentially, you need to figure out what vars are in scope, and then determine which information in them can be used. To determine what is in scope, you can use print_r(array_keys(get_defined_vars())); and then poke around in the individual vars.
An option is to do something in theme_preprocess_page. One option is to get the page data via page_manager_get_current_page(), poke around in there, and then add body classes as needed. Without knowing what you are doing, you essentially need to print_r the results somewhere, look at what you have, and go from there.

Resources