Contextual links létrehozása mezőkhöz

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.
Kategória: 
Leírás
Contextual links létrehozása mezőkhöz

A drupal 7 egyik komoly ux újdonsága, a contextual links. Ez gyakorlatilag azt jelenti, hogy egy adott elemen állva (pl egy blokkban, vagy views listában nodeokon) megjelenik néhány olyan link, amikkel az elemre tudunk hatni, például törölhetjük, szerkeszthetjük.

Esetemben konkrétan az volt a feladat, hogyha feltöltünk imagefielden keresztül egy képet, amit a media modul kezel, akkor a node megtekintésekor legyen egy ugyanilyen csini legördülő menü magukon a képeken, amivel egyből szerkeszthetjük azokat.

Na ez azért nem egy egyszerű mutatvány, legalábbis ha elsőre kell ilyet csinálni, de ha megvan a logika, akkor utána könnyebben megy.

Alapértelmezett contextual linkekként olyan útvonalakat tudunk megjeleníteni, amelyek minimum egy adott vonalból származnak. Kell még hozzá egy másik feltétel is, amit a modulfejlesztők a menu felépítésekor adhatnak meg, tehát a hook_menu-ban deklarált menütömbnek tartalmaznia kell a következő elemet:

[geshifilter-code] 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, [/geshifilter-code]

Ha ez nincsen, akkor nem fog megjelenni a contextual linkek között. Ezt könnyen áthághatjuk, de erről majd máskor.

Akkor lássunk neki! A következő lépésekre van szükségünk:

1. Hozzá kell adni a saját sminkdeklarációnkat ahhoz az elemhez, amihez contextual links-t akarunk kapcsolni.
2. Be kell regisztrálni a sminkdeklarációt.
3. Elő kell készíteni az adatokat a template fájlba íráshoz.
4. Létre kell hozni a szükséges template file-t.
5. Opcionális - belebabrálhatunk a most létrehozott linkekbe

Először a hook_entity_view_alter hookot kell megvalósítanunk. Ebben a hookban tudjuk módosítani a megjelenítendő elemek paramétereit, többek között befolyásolhatjuk, hogy hogyan, milyen sminkfüggvényeket használjon. Mi pontosan ezt fogjuk tenni.

Ez a funkció mindegy egyes entitás megjelenése előtt meghívódik. A $build változó tartalmazza azt a renderable array-t (wtf lehetne ez magyarul???), amiből az egyes elemek sminkelődni fognak. Ez a hook igen érzékeny, mivel akár egy user, akár egy comment akar kirajzolódni az oldalon, ez mindenképpen szaladni fog, szóval bánjunk vele úgy, mint egy hímes tojással, mert könnyen olyan adatokat babrálhatunk benne, ami nem is létezik.

A mi feladatunk ebben a funkcióban, hogy keressük meg azokat az elemeket, amelyek mezők, és media vagy image típusúak, és adjunk hozzá egy saját sminkfüggvényt, mégpedig #theme-wrapperként. Ez azt jelenti, hogy nem magát az elemet fogja a függvényünk sminkelni, hanem az elem köré építünk html-t. Valamint itt hozzuk a drupal tudtára, hogy márpedig nekünk ide contextual links kell.
Most szépen magyarul felkommentelem, ám ezt rendes kódban nem illik. :P

[geshifilter-drupal6]/* * Implements hook_view_entity_alter */ function mymodule_hooks_entity_view_alter(&$build, $type) { //Végigszaladunk az elemeken, amik nem paraméterek, tehát nem #-vel kezdődnek. foreach (element_children($build) as $field) { //Ez az a feltétel, ami nekünk kell. A media gallery tartalomtípust azért vesszük ki, // mert azt a media_gallery modul szolgáltatja, és ő csinál hozzá saját contextual linkeket. if (isset($build[$field]['#field_type']) && ($build[$field]['#field_type'] == 'image' || $build[$field]['#field_type'] == 'media') && $build['#bundle'] != 'media_gallery') { //Mivel egy mezőben akár több érték is lehet, tehát egy mezőhöz több kép is kapcsolható, // végigmegyünk az összesen, mert minden képnek saját menüt akarunk foreach (element_children($build[$field]) as $delta) { //Ez azért kell, mert lehetnek a tömbben olyan segédváltozók, //amik valójában nem egy-egy elemet jelentenek. if (is_numeric($delta)) { //És íme a lényeg: a contextual links deklaráció, magyarázat a kód alatt $build[$field][$delta]['#contextual_links']['field_image'] = array('media', array($build[$field]['#items'][$delta]['fid'])); //Ő lesz a mi kis saját sminkfüggvényünk. $build[$field][$delta]['#theme_wrappers'] = array('mymodule_contextual_links'); } } } } }[/geshifilter-drupal6]

Ezt a részt vesézzük ki, mert baromira nem egyértelmű elsőre:

[geshifilter-drupal6] $build[$field][$delta]['#contextual_links']['field_image'] = array( 'media', array($build[$field]['#items'][$delta]['fid']) ); [/geshifilter-drupal6]

Mi itt most egy field_image nevű contextual links elemet gyártunk A contextual links deklarációja egy tömb, ami minimum két értéket kíván. Az első az az útvonal, aminek a 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, paraméterrel ellátott gyermekei fognak megjelenni a menüben. A media útvonalhoz jelen esetben a media/[fid]/edit útvonal tartozik. A másodikban az útvonal argumentumait adjuk meg szintén egy tömbben. Itt és most azt mondtuk neki, hogy argumentumként használd az adott mezőhöz tartozó file azonosítóját.

Akkor a kövi, hozzuk létre a saját sminkdeklarációnkat. Ez itt olyan hülyén hangzik, ha jobbat mondasz, lecserélem. A lényeg, hogy a hook_theme megvalósításával tudatjuk a drupallal, hogy helló, nekünk van egy template fájlunk, használd légyszi.

[geshifilter-drupal6]/** * Implements hook_theme(). */ function mymodule_hooks_theme() { $items = array( //nézd csak vissza, 'mymodule_contextual_links', ezt adtuk meg az előbb a #theme_wrapper változónak 'mymodule_contextual_links' => array( 'render element' => 'element', //contextual_links_wrapper.tpl.php - ez lesz a fáljunk, amit létre fogunk hozni. 'template' => 'contextual_links_wrapper', ), ); return $items; }[/geshifilter-drupal6]

Na, mivel szépen akarunk csinálni mindent, nem csak úgy belehányunk fűt-fát a tpl.php fáljunkba, hanem előtte szépen előkészítjük az adatokat. Minden .tpl.php fájlhoz tartozik egy előfeldolgozó, aminek a neve [mymodule]_preprocess_[fájlneve].

@todo: ahogy ez most leírtam, rájöttem, hogy én nem így csináltam, de működik. Szóval most vagy nem tartozik automatikusan minden .tpl.php-hez előfeldogozó, hanem a _theme-ben beállított változóhoz tartozik (mymodule_contextual_links), vagy mindkettőt lehet használni, vagy egyszerűen én bénáztam, hogy kiléptem a namespaceből, és a templatenek is 'mymodule_contextual_links'-t kellett volna megadni, illetve mymodule_contextual_links.tpl.php-t használni.

Valósítsuk hát meg:

[geshifilter-drupal6]/** * Implements template_preprocess_contextual_links */ function mymodule_preprocess_mymodule_contextual_links(&$variables) { //Ezzel lehetőséget adunk más moduloknak is, hogy extra css classt adjanak a mi tpl.php-nkhez. if (isset($variables['element']['class'])) { $variables['classes_array'][] = $variables['element']['class']; } //Ő lesz maga a tartalom, tehát a renderelt, megsminkelt mező. Miután #theme-wrapperként adtunk //hozzá a sminkdeklarációnkat, ezért az eredeti tömb #children-ként szerepel benne $variables['content'] = $variables['element']['#children']; }[/geshifilter-drupal6]

Eddig csúcs, már csak a .tpl.php-nk hiányzik, hozzuk hát létre, és tegyük bele a kövit:

[geshifilter-drupal6]<?php /** * @file contextual_links_wrapper.tpl.php * Default template implementation to display contextual links around image fields. * */ ?> <div class="<?php print $classes; ?>" <?php print $attributes; ?>> <?php print render($title_prefix); ?> <?php print $content ?> <?php print render($title_suffix); ?> </div>[/geshifilter-drupal6]

Azért itt látszik, hogy sokat nem csináltunk. Ami a lényeg, az a $title_prefix, és $title_suffix. Hogy ez gyárilag mit csinál lövésem nincs. Valószínűnek tartom, hogy a drupal ezeket a változókat magának tartja fenn, ugyanis ezekben írja ki a contextual linkekhez szükséges html-t.

Ha ez megvan, akkor egy smink gyorstár ürítés, és már ott is vannak a képeken a csini linkek. És miután megtelt a node, a belebabrálásról máskor mesélek majd.

Hozzászólások

Hi there fantastic blog! Does

Hi there fantastic blog! Does running a blog like this take a great deal of work? I have very little understanding of programming however I was hoping to start my own blog soon. Anyway, if you have any ideas or tips for new blog owners please share. I understand this is off topic however I just wanted to ask. Appreciate it!|

Greetings from Florida! I'm

Greetings from Florida! I'm bored at work so I decided to check
out your site on my iphone during lunch break. I love
the knowledge you present here and can't wait to
take a look when I get home. I'm shocked at how fast your blog loaded on my mobile
.. I'm not even using WIFI, just 3G .. Anyhow, superb site!

Ridiculous story there. What

Ridiculous story there. What happened after? Thanks!

It's going to be end of mine

It's going to be end of mine day, however before end I am reading this great piece
of writing to increase my knowledge.

Simply desire to say your

Simply desire to say your article is as astonishing. The clarity for your
post is just great and that i can assume you're an expert in this subject.
Well together with your permission allow me to snatch your feed to
keep up to date with imminent post. Thank you one million and please continue the rewarding work.

Jay's Santa Clarita Tree Removal Kings
26174 Las Lanas Ct, Santa Clarita, CA 91355
661-464-1270
laureljefferson09875 [at] gmail [dot] com

Hi, i believe that i saw you

Hi, i believe that i saw you visited my website so i got
here to go back the desire?.I am attempting to
find issues to improve my website!I guess its good enough to
use a few of your ideas!!

I just couldn't leave your

I just couldn't leave your website prior to suggesting that I actually enjoyed the standard info an individual supply to your guests? Is going to be back continuously in order to check out new posts|

Thanks for one's marvelous

Thanks for one's marvelous posting! I seriously enjoyed
reading it, you're a great author.I will make sure to bookmark your blog and will eventually come back
someday. I want to encourage yourself to continue
your great job, have a nice afternoon!

When I originally commented I

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three emails with the same comment.
Is there any way you can remove me from that service?
Bless you!
Jim's San Jose Carpet Cleaning
780 S 3rd St, San Jose, CA 95112
3408-351-1063
colettemackie2641 [at] gmail [dot] com

Howdy! Someone in my Facebook

Howdy! Someone in my Facebook group shared this site with
us so I came to look it over. I'm definitely loving the information. I'm bookmarking and will
be tweeting this to my followers! Excellent blog and fantastic style and design.

I just like the helpful

I just like the helpful information you supply
in your articles. I'll bookmark your blog and take a look at again here
frequently. I am quite certain I will be informed a lot of new
stuff right right here! Good luck for the next!

I love your blog.. very nice

I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to
do it for you? Plz reply as I'm looking to create my own blog and would like to know where u got this
from. many thanks

Hello there! I know this is

Hello there! I know this is somewhat off topic but I was wondering if you knew where I could
get a captcha plugin for my comment form? I'm using the same blog platform
as yours and I'm having trouble finding one? Thanks a lot!

I enjoy what you guys tend to

I enjoy what you guys tend to be up too. This kind of clever work and
reporting! Keep up the very good works guys I've added you guys to my personal blogroll.

Hi, i think that i saw you

Hi, i think that i saw you visited my blog so i came to “return the favor”.I am trying to find things to improve my site!I suppose its ok to use some of your ideas!!|

Link exchange is nothing else

Link exchange is nothing else except it is just placing the other person's
blog link on your page at suitable place and other person will also
do similar in favor of you.

I have been browsing on-line

I have been browsing on-line greater than 3 hours as of late, but I never found any attention-grabbing article like yours.
It's pretty worth enough for me. In my opinion, if all web owners
and bloggers made good content as you did, the net will likely be a lot more useful
than ever before.

FlexBuild Huntington Beach Concrete Company
2520 Florida St, Huntington Beach, CA 92648
714-410-5374
suzannaweiss98719 [at] gmail [dot] com

I every time emailed this

I every time emailed this weblog post page to all my friends, since if like to read
it after that my contacts will too.

You really make it seem so

You really make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complex and very broad for me. I'm looking forward for your next post, I will try to get the hang of it!|

It's actually a nice and

It's actually a nice and useful piece of info. I'm satisfied that you shared this useful information with us.
Please keep us up to date like this. Thank you for sharing.

It's truly very complicated

It's truly very complicated in this busy life to listen news on Television, so I simply use web for that purpose, and take the
most up-to-date news.

each time i used to read

each time i used to read smaller articles or reviews which as well clear their motive, and that is
also happening with this piece of writing which I am reading now.

WOW just what I was searching

WOW just what I was searching for. Came here by searching for %meta_keyword%|

You could certainly see your

You could certainly see your skills in the article you
write. The world hopes for even more passionate writers like you who aren't afraid to mention how
they believe. All the time go after your heart.

FlexBuild Huntington Beach Concrete Company
2520 Florida St, Huntington Beach, CA 92648
714-410-5374
suzannaweiss98719 [at] gmail [dot] com

No matter if some one

No matter if some one searches for his vital thing, thus
he/she wishes to be available that in detail, thus that thing is maintained over here.

Hello, I want to subscribe

Hello, I want to subscribe for this webpage to get most
up-to-date updates, thus where can i do it please help.

I do not even know how I

I do not even know how I ended up here, but I thought this post was great.
I do not know who you are but certainly you're going to a famous blogger if you are not already ;) Cheers!

I love what you guys are

I love what you guys are usually up too. This
sort of clever work and coverage! Keep up the good works guys I've added
you guys to my personal blogroll.

I enjoy what you guys tend to

I enjoy what you guys tend to be up too. This type of clever work and reporting!
Keep up the awesome works guys I've incorporated you guys to blogroll.

This information is

This information is priceless. When can I find out more?|

Appreciating the persistence

Appreciating the persistence you put into your website and in depth information you offer.
It's great to come across a blog every once in a while
that isn't the same outdated rehashed information. Excellent read!
I've saved your site and I'm including your RSS feeds to my
Google account.

It's going to be end of mine

It's going to be end of mine day, but before ending I am reading this wonderful piece of writing to increase my know-how.

This paragraph gives clear

This paragraph gives clear idea in favor of the new visitors of blogging, that truly how to do blogging and site-building.

Jim's San Jose Carpet Cleaning
780 S 3rd St, San Jose, CA 95112
3408-351-1063
colettemackie2641 [at] gmail [dot] com

That is a really good tip

That is a really good tip especially to those fresh to the blogosphere.
Simple but very accurate info… Many thanks for sharing this one.

A must read article!
Jim's San Jose Carpet Cleaning
780 S 3rd St, San Jose, CA 95112
3408-351-1063
colettemackie2641 [at] gmail [dot] com

Everyone loves what you guys

Everyone loves what you guys are up too. This kind of clever work and reporting!
Keep up the great works guys I've included you guys to my own blogroll.

Hello there! Do you know if

Hello there! Do you know if they make any plugins to assist
with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing
very good gains. If you know of any please share. Thank you!

You actually make it seem so

You actually make it seem so easy with your presentation but
I find this topic to be really something which I think I would never understand.
It seems too complex and extremely broad for me. I'm
looking forward for your next post, I will try to get the hang of it!

This post provides clear idea

This post provides clear idea in favor of the new users of blogging, that truly how to do blogging.|

whoah this blog is

whoah this blog is magnificent i like reading your posts.
Keep up the great work! You understand, many people are
looking round for this info, you can aid them greatly.

What's Happening i'm new to

What's Happening i'm new to this, I stumbled upon this I've discovered It absolutely useful and it
has aided me out loads. I am hoping to contribute & aid other customers like its helped me.
Great job.

What's up to all, how is all,

What's up to all, how is all, I think every one is getting
more from this web site, and your views are fastidious in favor of new users.

It is appropriate time to

It is appropriate time to make some plans for the longer term and
it's time to be happy. I've learn this publish and if I may
I wish to suggest you some attention-grabbing issues or advice.
Maybe you could write subsequent articles referring to this article.
I wish to learn even more issues approximately it!

Howdy! Do you know if they

Howdy! Do you know if they make any plugins to protect against
hackers? I'm kinda paranoid about losing everything I've
worked hard on. Any recommendations?

Hello everyone, it's my first

Hello everyone, it's my first pay a quick visit at this web site, and article is in fact fruitful designed for me, keep up posting these articles or reviews.|

Thank you, I have recently

Thank you, I have recently been searching for info approximately this
subject for ages and yours is the greatest I have discovered so far.
But, what concerning the bottom line? Are you sure concerning
the source?

Do you have a spam issue on

Do you have a spam issue on this site; I also am a blogger, and I was wondering your situation;
many of us have created some nice methods and we are looking to exchange strategies with others, be sure
to shoot me an email if interested.

Hey there! I just want to

Hey there! I just want to offer you a big thumbs up for the great information you have right
here on this post. I will be coming back to your web site for more soon.

Greetings! Very useful advice

Greetings! Very useful advice in this particular post!
It is the little changes that produce the largest changes.
Thanks a lot for sharing!

It's impressive that you are

It's impressive that you are getting ideas from this article as well as from our discussion made at this time.|

Hey there, You've done an

Hey there, You've done an excellent job. I will definitely digg
it and personally suggest to my friends. I am confident they'll be benefited from this website.

Jay's Santa Clarita Tree Removal Kings
26174 Las Lanas Ct, Santa Clarita, CA 91355
661-464-1270
laureljefferson09875 [at] gmail [dot] com

Hozzászólás

A mező tartalma nem nyilvános.
  • Internal paths in double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or relative path.
  • Engedélyezett HTML elemek: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <del> <img>
  • A webcímek és email címek automatikusan linkekké alakulnak.
  • A sorokat és bekezdéseket a rendszer automatikusan felismeri.
  • Engedélyezett HTML elemek: <a> <blockquote> <br> <cite> <code> <dd> <del> <div> <dl> <dt> <em> <li> <ol> <p> <span> <strong> <ul>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <sql>. The supported tag styles are: <foo>, [foo].
  • Minden email cím át lesz alakítva ember által olvasható módon, vagy (ha a JavaScript engedélyezett) ki lesz cserélve kattintható, de biztonságos hivatkozásra.