際際滷

際際滷Share a Scribd company logo
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 lerendereld旦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旦ltdik. 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 elk辿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 indexeldik. Arra val坦,
    hogy extra inform叩ci坦 is indexeldj旦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
悪辿姻糸辿壊艶一

More Related Content

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 lerendereld旦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旦ltdik. 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 elk辿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 indexeldik. Arra val坦, hogy extra inform叩ci坦 is indexeldj旦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