How to programmatically get content type name in Drupal 8 - content-type

I'm working on Drupal 8. And I want to get content type machine name and label. Here is my code:
$cont_type = node_type_get_types();
foreach ($cont_type as $key => $value) {
$label = $value->name;
$machine_name = $key;
}
Here I got an error message : Cannot access protected property Drupal\node\Entity\NodeType::$name

In order to get current content type:
$node = \Drupal::routeMatch()->getParameter('node');
$typeName = $node->bundle();
$typeLabel = $node->getTitle();
There is an alternative method.
$node = \Drupal::request()->attributes->get('node')

<?php
use Drupal\node\Entity\NodeType;
?>
<?php
$all_content_types = NodeType::loadMultiple();
/** #var NodeType $content_type */
foreach ($all_content_types as $machine_name => $content_type) {
$label = $content_type->label();
}
?>

Solved using this code in template_preprocess_node()
$content_type = $node->type->entity->label();

$entityTypeManager = \Drupal::service('entity_type.manager');
$types = [];
$contentTypes = $entityTypeManager->getStorage('node_type')->loadMultiple();
foreach ($contentTypes as $contentType) {
$types[$contentType->id()] = $contentType->label();
}

The NodeType class inherits the label() method from the Entity class, use that function to get the content type label. See Entity::label.
$label = $value->label();

use {{ node.field_name.fieldDefinition.label }}

Please use the namespace
use Drupal\node\Entity\Node;

Related

Wordpress - How do I debug if the right parameters are added to my shortcode?

I am trying to debug if parameters are added to my shortcode. Currently, when I add a shortcode to my page and don't add a parameter it errors saying
Updating failed
But I would like to have a more detailed error. How would I do that? I'm working with code snippets and this is a front-end snippet. I noticed that I do get the error when I run the snippet in the back-end but then my entire WP UI disappears which isn't nice. Any tips?
<?php
add_shortcode('taxonomy_dropdown', 'do_taxonomy_dropdown');
function do_taxonomy_dropdown($atts = [])
{
$atts = array_change_key_case((array) $atts, CASE_LOWER);
if(do_validate_parameters($atts)){return;}
$taxonomy = $atts['taxonomy'];
$dependent = $atts['dependent'];
return;
}
function do_validate_parameters($atts = [])
{
$has_parameters = true;
if(!isset($atts['taxonomy'])){
echo "You forgot to add the taxonomy parameter.";
$has_parameters = false;
}
if(!isset($atts['dependent'])){
echo "You forgot to add the dependent parameter.";
$has_parameters = false;
}
return $has_parameters;
}
I think the issue you're having is with the initial portion not checking for the set variable. When I run it with error logging on I see that as an issue. Try this and see if it stops the issue.
add_shortcode('taxonomy_dropdown', 'do_taxonomy_dropdown');
function do_taxonomy_dropdown($atts = [])
{
$atts = array_change_key_case((array) $atts, CASE_LOWER);
if(do_validate_parameters($atts)){return;}
if ( isset($atts['taxonomy'])){
$taxonomy = $atts['taxonomy'];
}
if ( isset($atts['dependent'])){
$dependent = $atts['dependent'];
}
return;
}
function do_validate_parameters($atts = [])
{
$has_parameters = true;
if(!isset($atts['taxonomy'])){
echo "<p>You forgot to add the taxonomy parameter.</p>";
$has_parameters = false;
}
if(!isset($atts['dependent'])){
echo "<p>You forgot to add the dependent parameter.</p>";
$has_parameters = false;
}
return $has_parameters;
}

how to remove attribute value from custom search form in drupal 7

I have installed custom search Module for search block. in the search form there is one text field having value attribute with null value. which is showing the Accessibility issue, that's why i want to remove it. what should i do for this. please help.
can any one solve this type of issue.
Please see the source code snapshot.
snapshot of source code
For text fields the value attribute is forcibly added in theme_textfield(), so the only way would be to override that function in your theme and remove that bit of code:
function YOURTHEME_textfield($variables) {
$element = $variables['element'];
$element['#attributes']['type'] = 'text';
// remove value form array
element_set_attributes($element, array('id', 'name', 'size', 'maxlength'));
_form_set_class($element, array('form-text'));
$extra = '';
if ($element['#autocomplete_path'] && !empty($element['#autocomplete_input'])) {
drupal_add_library('system', 'drupal.autocomplete');
$element['#attributes']['class'][] = 'form-autocomplete';
$attributes = array();
$attributes['type'] = 'hidden';
$attributes['id'] = $element['#autocomplete_input']['#id'];
$attributes['value'] = $element['#autocomplete_input']['#url_value'];
$attributes['disabled'] = 'disabled';
$attributes['class'][] = 'autocomplete';
$extra = '<input' . drupal_attributes($attributes) . ' />';
}
$output = '<input' . drupal_attributes($element['#attributes']) . ' />';
return $output . $extra;
}
Hope this helps your...
You can remove it by using jquery. Use the below code.
$('.custom-search-box').removeAttr('value');
i guess this helps you.

Is that possible to use laravel blade outside the view folder?

I got a wordpress blog inside the public sub folder.
I wanted to use same layout with the laravel view that using blade.
Is there anyway to achieve that?
Yous just need to add your paths to app/config/view.php and blade will automatically find them
You can define a custom namespace for easier usage:
// Register your custom namespace in your AppServiceProvider in the boot() method
view()->addNamespace('custom_views', app_path('custom_path'));
// usage:
view('custom_views::some.view.name')
I managed to do this with the following function:
function bladeCompile ($from, $to, $data)
{
$fs = new \Illuminate\Filesystem\Filesystem;
$b = new \Illuminate\View\Compilers\BladeCompiler($fs, __DIR__);
$src = $b->compileString (file_get_contents($from));
$isPhp = false;
if (substr( $src, 0, 5 ) === "<?php")
{
$isPhp = true;
$src = substr($src, 5);
}
$tempFileName = tempnam("/tmp", "blade-compile");
file_put_contents($tempFileName, $src);
ob_start();
extract($data);
include $tempFileName;
$out = ob_get_clean();
if ($isPhp)
{
$out = '<?php'.$out;
}
file_put_contents($to, $out);
}
And then use with:
$data = array ( // equivalent to the 'with' function.
'parameter' => 'value';
);
bladeCompile ('input.blade.file', 'result.file', $data);

why the overwrite doesn't have the output?

function rate_preprocess_rate_template_emotion(&$variables) {
extract($variables);
$buttons = array();
foreach ($links as $link) {
$button = theme('rate_button', $link['text'], $link['href'], 'rate-emotion-btn');
$button .= $link['votes'];
$buttons[] = $button;
}
$variables['buttons'] = $buttons;
$info = array();
........
now i want to add <br/><span class="pollunm">around the </span>. i put this code in my theme template.php.but it doesn't output the span tags.
function mytheme_preprocess_rate_template_emotion(&$variables) {
$link['votes']='<br/><span class="pollunm">'.$link['votes'].' </span>';
}
This is not a solution. Just the step for you to debug.
First make sure that function mytheme_preprocess_rate_template_emotion(&$variables) is getting called by putting a dpm(install devel module) in the function.
And inside function rate_preprocess_rate_template_emotion(&$variables) they are using foreach ($links as $link). So make sure that whether you want to do it for all links are just one link.
Within function mytheme_preprocess_rate_template_emotion(&$variables) put a dpm($variables); and find out which are the variables available to you and what are their values. It might help you.

how to get all the menu items below a certain parent in drupal?

I really only need the mlid and title text for the first level below a certain menu item. Here's what I'm doing at the moment. (It works, but I suspect there may be a more drupal-y way.):
/**
* Get all the children menu items below 'Style Guide' and put them in this format:
* $menu_items[mlid] = 'menu-title'
* #return array
*/
function mymod_get_menu_items() {
$tree = menu_tree_all_data('primary-links');
$branches = $tree['49952 Parent Item 579']['below']; // had to dig for that ugly key
$menu_items = array();
foreach ($branches as $menu_item) {
$menu_items[$menu_item['link']['mlid']] = $menu_item['link']['title'];
}
return $menu_items;
}
Is there?
Actually there is an easy way to get that information by using menu_build_tree():
// Set $path to the internal Drupal path of the parent or
// to NULL for the current path
$path = 'node/123';
$parent = menu_link_get_preferred($path);
$parameters = array(
'active_trail' => array($parent['plid']),
'only_active_trail' => FALSE,
'min_depth' => $parent['depth']+1,
'max_depth' => $parent['depth']+1,
'conditions' => array('plid' => $parent['mlid']),
);
$children = menu_build_tree($parent['menu_name'], $parameters);
$children contains all information you need. menu_build_tree() checks access or translation related restrictions too so you only get what the user really should see.
afaik, there isn't (i hope i am wrong).
for the while, instead of digging for ugly keys, you can turn your function into a more abstract helper function by simply adding a foreach ($tree). then you can use your own logic to output what you want (mlid, in this case). here is my suggestion:
/**
* Get the children of a menu item in a given menu.
*
* #param string $title
* The title of the parent menu item.
* #param string $menu
* The internal menu name.
*
* #return array
* The children of the given parent.
*/
function MY_MODULE_submenu_tree_all_data($title, $menu = 'primary-links') {
$tree = menu_tree_all_data($menu);
foreach ($tree as $branch) {
if ($branch['link']['title'] == $title) {
return $branch['below'];
}
}
return array();
}
Have you looked into the Menu block module? Some more details about this module (from its project page):
... have you ever used the Main and Secondary menu links feature on your theme and wondered “how the hell do I display any menu items deeper than that?”
Well, that’s what this module does. It provides configurable blocks of menu trees starting with any level of any menu. And more!
So if you’re only using your theme’s Main menu links feature, you can add and configure a “Main menu (levels 2+)” block. That block would appear once you were on one of the Main menu’s pages and would show the menu tree for the 2nd level (and deeper) of your Main menu and would expand as you traversed down the tree. You can also limit the depth of the menu’s tree (e.g. “Main menu (levels 2-3)”) and/or expand all the child sub-menus (e.g. “Main menu (expanded levels 2+)”).
I use this :
Just add your path and eventualy the menu and it will give you the child.
function MY_MODULE_submenu_tree_all_data($path, $menu = 'main-menu', $curr_level = 0, $rebuilt_path='', $childtree = array()) {
$tree = menu_tree_all_data($menu);
$args = explode('/', $path);
$rebuilt_path = empty($rebuilt_path) ? $args[$curr_level] : $rebuilt_path . '/' . $args[$curr_level];
foreach ($tree as $branch) {
if ($branch['link']['link_path'] == $rebuilt_path) {
$childtree = $branch['below'];
if ($rebuilt_path != $path) {
$curr_level++;
MY_MODULE_submenu_tree_all_data($path, $menu, $curr_level, $rebuilt_path, $childtree);
}
}
}
$items = array();
foreach ($childtree as $child) {
$items[] = l($child['link']['title'], $child['link']['link_path']);
}
return theme('item_list', array('items' => $items, 'attributes' => array(), 'type' => 'ul'));
}
Here's a helper function to return a whole subtree of a menu, starting at a specified mlid. Some of the other posts only return the direct descendants of the current item; this will return ALL descendants.
By default it gives you the subtree starting with the current page, but you can pass in any menu tree (as returned by menu_build_tree) and any mlid.
function _menu_build_subtree($menu=NULL,$mlid=NULL) {
if ($menu == NULL || $mlid == NULL) {
$parent = menu_link_get_preferred();
}
$menu = !is_null($menu) ? $menu : menu_build_tree($parent['menu_name']);
$mlid = !is_null($mlid) ? $mlid : $parent['mlid'];
foreach ($menu as $branch) {
if ($branch['link']['mlid'] == $mlid) {
return $branch;
}
$twig = _menu_build_subtree($branch['below'],$mlid);
if ($twig) { return $twig; }
}
return array();
}

Resources