SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
Drupal NodeAPI
Alapfogalmak
●   node object:
    ●   {node} tábla
    ●   hookok által adott plussz dolgok
●   node id:
    ●   egyedi azonosítója a node-nak
    ●   nid néven szerepel az adatbázistáblákban
Alapműveletek
●   betöltés: node_load($nid)
●   mentés: node_save($node)
●   törlés: node_delete($nid)
●   megjelenítés (HTML-t ad vissza):
    node_view($node)
{node} tábla
                                     Table "public.node"
 Column    |           Type           |                      Modifiers
-----------+------------------------+----------------------------------------------------
nid        | integer                  | not null default nextval('node_nid_seq'::regclass)
vid        | int_unsigned             | not null default 0
type       | character varying(32)    | not null default ''::character varying
language   | character varying(12)    | not null default ''::character varying
title      | character varying(255) | not null default ''::character varying
uid        | integer                  | not null default 0
status     | integer                  | not null default 1
created    | integer                  | not null default 0
changed    | integer                  | not null default 0
comment    | integer                  | not null default 0
promote    | integer                  | not null default 0
moderate   | integer                  | not null default 0
sticky     | integer                  | not null default 0
tnid       | int_unsigned             | not null default 0
translate | integer                   | not null default 0
Felosztás
●   Meglévő tartalomtípus módosítása
●   Új tartalomtípus hozzáadása
Új tartalomtípus definiálása
Kötelező hookok
●   hook_node_info()
●   hook_perm()
●   hook_access() *
●   hook_form() *
Opcionális hookok
●   hook_insert() *
●   hook_update() *
●   hook_delete() *
●   hook_validate() *
●   hook_view() *
●   hook_load() *
hook_node_info()
hook_node_info() {
    return array(
         'foobar' => $tulajdonsagok;
    );
}
Kötelező elemek
array(
     'name' => t('Könnyen olvasható név'),
     'module' => 'content_type_neve',
     'description' => t('hosszas leírás...'),
);
Opcionális elemek
array(
     'help' => t('help szöveg...'),
     'has_title' => TRUE,
     'title_label' => t('Title'),
     'has_body' => TRUE,
     'body_label' => t('Body'),
     'min_word_count' => 0,
     'locked' => TRUE,
);
hook_perm()
function hook_perm() {
    return array(
         'create example content',
         'delete own example content',
         'delete any example content',
         'edit own example content',
         'edit any example content',
    );
}
hook_access()
function hook_access($op, $node, $account) {
    if ($op == 'create') {
        return user_access('create example content', $account);
    }
    if ($op == 'update') {
  if (user_access('edit any example content', $account) || (user_access('edit own example content', $account) &&
($account->uid == $node->uid))) {
            return TRUE;
        }
    }
    if ($op == 'delete') {
  if (user_access('delete any example content', $account) || (user_access('delete own example content', $account) &&
($account->uid == $node->uid))) {
            return TRUE;
        }
    }
    if ($op == 'view') {
        return NULL;
    }
}
hook_access()
●   Visszatérési érték:
    ●   TRUE – engedély megadva
    ●   FALSE – engedély megtagadva
    ●   NULL – nem rendelkezik, így majd a node_access
        tábla dönt, vagy valamelyik más access control
        modul
hook_form()
function hook_form(&$node, $form_state) {
   $form = array();
   $type = node_get_types('type', $node);
   if($type->has_title) {
       $form['title'] = array(
            '#type' => 'textfield',
            '#title' => check_plain($type->title_label),
            '#required' => TRUE,
            '#default_value' => $node->title,
            '#weight' => -5,
       );
   }
hook_form()
    if($type->has_body) {
             $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
    }
    $form['color'] = array(
         '#type' => 'textfield',
         '#title' => t('Color'),
         '#default_value' => isset($node->color) ? $node->color : '',
        );
        $form['quantity'] = array(
         '#type' => 'textfield',
         '#title' => t('Quantity'),
         '#default_value' => isset($node->quantity) ? $node->quantity : 0,
         '#size' => 10,
         '#maxlength' => 10,
        );
        return $form;
}
hook_insert()
function hook_insert($node) {
    db_query('INSERT INTO {node_foo}(nid, vid, extra)
    VALUES(%d, %d, '%s')', $node->nid, $node->vid,
    $node->extra);
}
hook_update()
function hook_update($node) {
    if($node->revision) {
        hook_insert($node);
    } else {
        db_query('UPDATE {node_foo} SET extra = '%s'
        WHERE nid = %d', $node->extra, $node->nid);
    }
}
hook_delete()
function hook_delete($node) {
    db_query('DELETE FROM {node_foo} WHERE nid
    = %d', $node->nid);
}
hook_validate()
function hook_validate($node, &$form) {
    if(something_is_wrong_with($form)) {
        form_set_error(...);
    }
}
hook_view()
function hook_view($node, $teaser = FALSE, $page = FALSE) {
    $node = node_prepare($node, $teaser);
     $node->content['myfield'] = array(
      '#value' => theme('node_example_order_info', $node),
      '#weight' => 1,
     );


     return $node;
}
hook_load()
function hook_load($node) {
    $additions = db_fetch_object(db_query('SELECT *
    FROM {node_foo} WHERE nid = %d AND vid =
    %d', $node->nid, $node->vid));
    return $additions;
}
Theme függvény
function theme_node_example_order_info($node) {
    $output = '<div class="node_example_order_info">';
 $output .= t('The order is for %quantity %color items.',
array('%quantity' => check_plain($node->quantity),
'%color' => check_plain($node->color)));
    $output .= '</div>';
    return $output;
}
Példa
●   Blog modul (könnyen olvasható, rövid)
●   Book modul
Meglévő tartalomtípus módosítása
hook_nodeapi()
●   Az egyik legcsúnyább ős-Drupal függvény, ami
    benne maradt a Drupal 6-ban
●   Szignatúra
      function hook_nodeapi(&$node, $op, $a3 = NULL,
      $a4 = NULL)
$op lista
●   alter: $node->content lerenderelődött, a body
    és a teaser HTML-t tartalmaz. Ezt az $op-ot
    nyers szövegműveletekre (cserélés, szűrés)
    szabad használni
●   delete: a node törlésénél hajtódik végre
●   delete revision: node revision törlése (erre az
    egy $op-ra saját tartalomtípusnál is lehet
    szükség, mivel csak így lehet revision-t törölni)
$op lista
●   insert: a node létrejön
●   load: a node betöltődik. Itt lehet hozzáadni
    dolgokat a node objecthez.
●   prepare: a node megjelenni készül egy add/edit
    formon
●   prepare translation: a node fordításhoz való
    klónozásakor fut le
●   print: a node előkészül nyomtatásra (példa:
    book modulban)
$op lista
●   rss item: RSS generálódik. Lásd
    comment_nodeapi() függvény
●   search result: a node keresési eredményként
    lesz megjelenítve. Csak plussz információ
    átadására lehet ezt használni.
●   presave: a node már validálódott, de még nincs
    mentve
$op lista
●   update: node frissül
●   update index: a node indexelődik. Arra való,
    hogy extra információ is indexelődjön (amit nem
    ad át a hook_view())
●   validate: ugyanaz, mint a hook_validate()
●   view: közvetlen a hook_view() után lesz
    meghívva a node megjelenítésekor
$a3
●   view: $teaser
●   validate: $form
$a4
●   view: $page
Visszatérési értékek
●   A "presave", "insert", "update", "delete", "print"
    és "view" $op esetén nincs.
●   “load” $op esetén egy asszociatív tömb(!), ami
    hozzáadódik a node objecthez
Kérdések

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Dcourse nodeapi

  • 2. Alapfogalmak ● node object: ● {node} tábla ● hookok által adott plussz dolgok ● node id: ● egyedi azonosítója a node-nak ● nid néven szerepel az adatbázistáblákban
  • 3. Alapműveletek ● betöltés: node_load($nid) ● mentés: node_save($node) ● törlés: node_delete($nid) ● megjelenítés (HTML-t ad vissza): node_view($node)
  • 4. {node} tábla Table "public.node" Column | Type | Modifiers -----------+------------------------+---------------------------------------------------- nid | integer | not null default nextval('node_nid_seq'::regclass) vid | int_unsigned | not null default 0 type | character varying(32) | not null default ''::character varying language | character varying(12) | not null default ''::character varying title | character varying(255) | not null default ''::character varying uid | integer | not null default 0 status | integer | not null default 1 created | integer | not null default 0 changed | integer | not null default 0 comment | integer | not null default 0 promote | integer | not null default 0 moderate | integer | not null default 0 sticky | integer | not null default 0 tnid | int_unsigned | not null default 0 translate | integer | not null default 0
  • 5. Felosztás ● Meglévő tartalomtípus módosítása ● Új tartalomtípus hozzáadása
  • 7. Kötelező hookok ● hook_node_info() ● hook_perm() ● hook_access() * ● hook_form() *
  • 8. Opcionális hookok ● hook_insert() * ● hook_update() * ● hook_delete() * ● hook_validate() * ● hook_view() * ● hook_load() *
  • 9. hook_node_info() hook_node_info() { return array( 'foobar' => $tulajdonsagok; ); }
  • 10. Kötelező elemek array( 'name' => t('Könnyen olvasható név'), 'module' => 'content_type_neve', 'description' => t('hosszas leírás...'), );
  • 11. Opcionális elemek array( 'help' => t('help szöveg...'), 'has_title' => TRUE, 'title_label' => t('Title'), 'has_body' => TRUE, 'body_label' => t('Body'), 'min_word_count' => 0, 'locked' => TRUE, );
  • 12. hook_perm() function hook_perm() { return array( 'create example content', 'delete own example content', 'delete any example content', 'edit own example content', 'edit any example content', ); }
  • 13. hook_access() function hook_access($op, $node, $account) { if ($op == 'create') { return user_access('create example content', $account); } if ($op == 'update') { if (user_access('edit any example content', $account) || (user_access('edit own example content', $account) && ($account->uid == $node->uid))) { return TRUE; } } if ($op == 'delete') { if (user_access('delete any example content', $account) || (user_access('delete own example content', $account) && ($account->uid == $node->uid))) { return TRUE; } } if ($op == 'view') { return NULL; } }
  • 14. hook_access() ● Visszatérési érték: ● TRUE – engedély megadva ● FALSE – engedély megtagadva ● NULL – nem rendelkezik, így majd a node_access tábla dönt, vagy valamelyik más access control modul
  • 15. hook_form() function hook_form(&$node, $form_state) { $form = array(); $type = node_get_types('type', $node); if($type->has_title) { $form['title'] = array( '#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5, ); }
  • 16. hook_form() if($type->has_body) { $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count); } $form['color'] = array( '#type' => 'textfield', '#title' => t('Color'), '#default_value' => isset($node->color) ? $node->color : '', ); $form['quantity'] = array( '#type' => 'textfield', '#title' => t('Quantity'), '#default_value' => isset($node->quantity) ? $node->quantity : 0, '#size' => 10, '#maxlength' => 10, ); return $form; }
  • 17. hook_insert() function hook_insert($node) { db_query('INSERT INTO {node_foo}(nid, vid, extra) VALUES(%d, %d, '%s')', $node->nid, $node->vid, $node->extra); }
  • 18. hook_update() function hook_update($node) { if($node->revision) { hook_insert($node); } else { db_query('UPDATE {node_foo} SET extra = '%s' WHERE nid = %d', $node->extra, $node->nid); } }
  • 19. hook_delete() function hook_delete($node) { db_query('DELETE FROM {node_foo} WHERE nid = %d', $node->nid); }
  • 20. hook_validate() function hook_validate($node, &$form) { if(something_is_wrong_with($form)) { form_set_error(...); } }
  • 21. hook_view() function hook_view($node, $teaser = FALSE, $page = FALSE) { $node = node_prepare($node, $teaser); $node->content['myfield'] = array( '#value' => theme('node_example_order_info', $node), '#weight' => 1, ); return $node; }
  • 22. hook_load() function hook_load($node) { $additions = db_fetch_object(db_query('SELECT * FROM {node_foo} WHERE nid = %d AND vid = %d', $node->nid, $node->vid)); return $additions; }
  • 23. Theme függvény function theme_node_example_order_info($node) { $output = '<div class="node_example_order_info">'; $output .= t('The order is for %quantity %color items.', array('%quantity' => check_plain($node->quantity), '%color' => check_plain($node->color))); $output .= '</div>'; return $output; }
  • 24. Példa ● Blog modul (könnyen olvasható, rövid) ● Book modul
  • 26. hook_nodeapi() ● Az egyik legcsúnyább ős-Drupal függvény, ami benne maradt a Drupal 6-ban ● Szignatúra function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
  • 27. $op lista ● alter: $node->content lerenderelődött, a body és a teaser HTML-t tartalmaz. Ezt az $op-ot nyers szövegműveletekre (cserélés, szűrés) szabad használni ● delete: a node törlésénél hajtódik végre ● delete revision: node revision törlése (erre az egy $op-ra saját tartalomtípusnál is lehet szükség, mivel csak így lehet revision-t törölni)
  • 28. $op lista ● insert: a node létrejön ● load: a node betöltődik. Itt lehet hozzáadni dolgokat a node objecthez. ● prepare: a node megjelenni készül egy add/edit formon ● prepare translation: a node fordításhoz való klónozásakor fut le ● print: a node előkészül nyomtatásra (példa: book modulban)
  • 29. $op lista ● rss item: RSS generálódik. Lásd comment_nodeapi() függvény ● search result: a node keresési eredményként lesz megjelenítve. Csak plussz információ átadására lehet ezt használni. ● presave: a node már validálódott, de még nincs mentve
  • 30. $op lista ● update: node frissül ● update index: a node indexelődik. Arra való, hogy extra információ is indexelődjön (amit nem ad át a hook_view()) ● validate: ugyanaz, mint a hook_validate() ● view: közvetlen a hook_view() után lesz meghívva a node megjelenítésekor
  • 31. $a3 ● view: $teaser ● validate: $form
  • 32. $a4 ● view: $page
  • 33. Visszatérési értékek ● A "presave", "insert", "update", "delete", "print" és "view" $op esetén nincs. ● “load” $op esetén egy asszociatív tömb(!), ami hozzáadódik a node objecthez